Code from text not working

Is this because Im using Dev-C++ 4.9.9.2? Is Dev-C++ a good IDE to use for C++?


#include <iostream> // allows program to perform input and output

using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl

// function main begins program execution
int main()
{
int number1; // first integer to compare
int number2; // second integer to compare

cout << "Enter two integers to compare: "; // prompt user for data

cin >> number1 >> number2; // read two integers from user

if ( number1 == number2 )
cout << number1 << " == " << number2 << endl;

if ( number1 != number2 )
cout << number1 << " != " << number2 << endl;

if ( number1 < number2 )
cout << number1 << " < " << number2 << endl;

if ( number1 > number2 )
cout << number1 << " > " << number2 << endl;

if ( number1 <= number2 )
cout << number1 << " <= " << number2 << endl;

if ( number1 >= number2 )
cout << number1 << " >= " << number2 << endl;

cin.get();

return 0; // indicate that program ended successfully

} // end function main
I believe the problem is in the following line:

cin >> number1 >> number2;

I think that's where the problem lies because if I give number1 a value when it is declared, the code works.
What is happening instead of what you are expecting? And use code tags please.
Sorry, I dont know what a code tag is. The program just opens and closes right away. I pretty much want to know how to get input from the user and store them under variables.
Oh, I think I figured out how to use a code tag.
Last edited on
Im pretty sure Dev-C++ isnt recognizing the "cin" command.
Its quite possible that cin.get(); is not keeping the DOS window open.
Try:
1
2
3
4
char q;
cin>>q;
return o;
}

quick question, is it necessary to have:
1
2
3
using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl 


in your program? i think if you use using namespace std; after your include, it will save you lines of code and unnecessary typing. I could be wrong. Also, after the user initializes the variable, and you apply the value the user assigned to the variable, i usually add cin.ignore(); after it. cin.ignore(); ignores the enter key, because the enter key is used to shut down the program. So I'd place it right after cin >> number1 >> number2;, on a new line. I also use Dev-C++ and I had that problem the first time. Please, if I am wrong someone correct me. I am new to C++.

EDIT: Tested with cin.ignore(); and worked.
Last edited on
@xitan

It's just better to use what you need from the namespace, rather than use the whole namespace itself since it can cause some errors.
closed account (z05DSL3A)
about namespace using declarations
http://www.cplusplus.com/forum/beginner/9181/
ok so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
    int x;
    
    std::cout << "Please enter a integer type number." << endl;
    std::cin >> x;
    std::cout << "The integer type number you entered is: " << x << endl;
    std::cin.ignore();
    
    std::cin.get();
    return (0);
}


would be a good example of declaring standards from the include that will only be used correct? im not sure of lines 10-13, if it's necessary to include std:: before the statement.
In lines 2-4, you have already introduced cin, cout and endl from the std namespace into global variable scope, so you can use them directly, as you have done with endl.
@Beibin would it make a difference if i introduced them as local? and if i did, in the sample program above, i would just declare them within the main function correct?
Last edited on
I'm not sure that's possible, but why would you want to do that?
Last edited on
It is possible:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

void testFunction(void);

int main(void)
{
  using std::cout;
  cout << "cout from main()\n\n";
  testFunction();
  std::cin.ignore();
  return 0;
}

void testFunction(void)
{
  /*
   * Compile-time error:
   *
   * In function `void testFunction()':
   * `cout' was not declared in this scope
   */
  cout << "cout from testFunction()\n\n";
}


Here, cout was introduced only inside the main function, not globally. Therefore, trying to access it outside of main will prevent you from compiling the program.
@Beibin Wow, thanks, very clear example. Exactly what I was looking for. Thanks man.
Topic archived. No new replies allowed.