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.
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 usingnamespace 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++.
#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?
#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.