That code is crap. Throw it away.
(It won't compile on
any compiler. You are better off trying to figure out how to do it yourself anyway.)
Everything you
do must be in a function. When the user starts your program, the
main() function runs first. Hence, a basic program always starts with it:
1 2 3 4 5 6 7
|
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!\n";
}
|
For anything with strings, include <string>.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
string users_name;
cout << "What is your name? ";
getline( cin, users_name );
cout << "Hello " << users_name << "!\n";
}
|
Remember that whenever you ask the user for something, he will press ENTER after that input. So if you plan to ask for
both strings and (things like numbers), you must pay attention to getting rid of that ENTER key. The
getline() function gets rid of it. All the stuff like
cin>>foo
does not, so you have to make sure to fix that at the right places.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
unsigned age;
double favorite_number;
string name;
cout << "What is your age? ";
cin >> age; // does not read the ENTER key, but that's okay, our next input is:
cout << "What is your favorite number? ";
cin >> double; // skips the previous ENTER key, does not read the one after the user's favorite number.
cout << "What is your name? ";
// Except next we want to read a string, so we have to get rid of that ENTER key that's sitting there in the input waiting to be read.
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
// Okay, now we can read a string, ending with (and throwing away) the next ENTER key press
getline( cin, name );
cout << "Okay, " << name << ", your favorite number ";
if (age == favorite_number) cout << "is";
else cout << "is not";
cout << " your favorite number. What's up with that?\n";
}
|
(By the way, you should be compiling and running these to see what is happening. This is the fastest crash-course you will ever get.)
Those "cin" and "cout" things are each a "stream". So is a file. To handle a file, include <fstream> and use it the same. The only thing is you have to specifically open a file before you can use it.
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 <fstream>
#include <limits>
#include <iostream>
#include <string>
using namespace std;
int main()
{
unsigned age;
string name;
ifstream fin( "a_file.txt" ); // ifstream for reading, ofstream for writing
// The file is presumably two lines long (at least):
// the first line has a whole number; the second line has a name.
fin >> age;
fin.ignore( numeric_limits<streamsize>::max(), '\n' );
getline( fin, name );
cout << "Hello " << name << ", you are " << age << " years old.\n";
// You don't have to call fin.close() here -- when the "fin" object is
// destroyed, the file will be closed for you.
}
|
Make sure to refer to the tutorial for more information, like arrays and "if" and "while" control constructs and the like.
http://www.cplusplus.com/doc/tutorial/
Hope this helps.