problem with cin>> in c++ using code blocks

Aug 15, 2014
3
0
4,510
0
hello i am new to c++ and i am using code blocks to learn it. i typed the following code:
#include <iostream>
using namespace std;

int main() {
int x=1;
int number;

while (x<=5){
cin >> number;
x++;
}

return 0;
}

the cin>> doesn't seem to work i.e the console box appears but i can't input any values

please help
 

Pinhedd

Distinguished
Moderator


Code::Blocks is an IDE, not a compiler. You should have chosen a compiler infrastructure when you created the project. It should support all the major ones on Windows including MinGW, MSVC++, Borland C++, and Intel C++.

I tested your code using ICPC (Intel's C++ compiler), clang++, and g++ on Linux and all worked well. I also tested it using MSVC++ on Windows and it also worked as expected

 
Aug 15, 2014
3
0
4,510
0




All i know is that according to the code that i've written above i'm supposed to type 5 numbers on the blank black box or screen (is it called the console box or application) until the box displays something like this:

process returned 0 <0x0> execution time : 5.59 s
press any key to continue

All i see is the black screen appearing and displaying " press any key to continue " even before i have typed any numbers
 

Ijack

Distinguished
Are you absolutely sure that you have typed the code exactly as here? It looks fine and I have pasted it and compiled it to check. It does exactly what you want it to.

Which operating system and C++ compiler are you using (not that it should matter)?
 
Aug 15, 2014
3
0
4,510
0


yes i have typed the code exactly as here. i am using windows 7 and the compiler is Code::Blocks
 

Pinhedd

Distinguished
Moderator


Code::Blocks is an IDE, not a compiler. You should have chosen a compiler infrastructure when you created the project. It should support all the major ones on Windows including MinGW, MSVC++, Borland C++, and Intel C++.

I tested your code using ICPC (Intel's C++ compiler), clang++, and g++ on Linux and all worked well. I also tested it using MSVC++ on Windows and it also worked as expected

 

Ijack

Distinguished
I only tested with gcc on Linux. But just looking at the code it has to work.

My money is still on a typo in the code that isn't present in the version shown here (for example "x >= 5" instead of "x <= 5").
 

Pinhedd

Distinguished
Moderator


Some C compilers are a bit pedantic about the main entry point. This is usually accompanied by a warning though. I know that C++ is significantly more flexible but I wanted to test it to be sure.

`x >= 5` is a good possibility though
 
Thread starter Similar threads Forum Replies Date
F Apps General Discussion 5
J Apps General Discussion 0
P Apps General Discussion 4
I Apps General Discussion 2
kikani.kautik Apps General Discussion 2
synoptic12 Apps General Discussion 3
S Apps General Discussion 5
D Apps General Discussion 1
S Apps General Discussion 1
P Apps General Discussion 1
F Apps General Discussion 1
A Apps General Discussion 5
S Apps General Discussion 1
henrytcasey Apps General Discussion 2
P Apps General Discussion 1
A Apps General Discussion 1
S Apps General Discussion 1
V Apps General Discussion 1
S Apps General Discussion 8
O Apps General Discussion 1

ASK THE COMMUNITY