C++ Question

Status
Not open for further replies.

TheJediSlayer

Posts: 164   +0
Well, I'm a beginner at C++ and I had a question to ask. It's about the \n and it messing up my code. I'm not sure why but when I tried to compile my program; it wouldn't go and gave me a bunch of errors. After trying to find out the problem, I finally got it, by removing \n in cout<<"Text to output"; it worked. However, I'm a bit confused as to why it worked before and not now. I am still using the same compiler as before, too.

Tutorials code:

#include <iostream>

using namespace std;

int main() // Most important part of the program!
{
int age; // Need a variable...

cout<<"Please input your age: "; // Asks for age
cin>> age; // The input is put in age
cin.ignore(); // Throw away enter
if ( age < 100 ) { // If the age is less than 100
cout<<"You are pretty young!\n"; // Just to show you it works...
}
else if ( age == 100 ) { // I use else just to show an example
cout<<"You are old\n"; // Just to show you it works...
}
else {
cout<<"You are really old\n"; // Executed if no other statement is
}
cin.get();
}

This was my code:
#include <iostream>

using namespace std;

int main()
{
int age;

cout<<"Please enter your current age: ";
cin>> age;
cin.ignore();
if ( age = 100 ) {
cout<<"Wow, you are very young!";
}
else if ( age < 100 ) {
cout<<"You are very old!";
}
else {
cout<<"You did not input an answer!:";
}
cin.get();
}

Reference:
http://www.cprogramming.com/tutorial/lesson2.html

Any help would be much appreciated,

Tyler
 
And what were the "bunch of errors".
Hint: the error messages from compilers are actually useful and help you to find out what is wrong :p

Also, what compiler are you using and how did you try to compile your program?

Assuming that not recognising the escape sequences is some sort of a compiler quirk, use the endl directive for line breaks instead. Something like "cout << "Some text here" << endl;"
 
Well, unfortunately I didn't save any the errors I got, as I was more keen at the time to just trying to fix them; which I did. However, the major error that I came up with, was this error with the \n. Anyways, I'm using Watcom version: open-watcom-f77-win32-1.7a.exe How did I try and compile the program? Well, the way they've shown me how to compile the program. Simply put the code into the text editor, run the .cpp file by pressing the F5 key, and wait and see if it was successful.

Other information:
Target Environment: Win32
Image Type: Character Mode Executable (.exe)
 
@_@ I've gotten several programs to work though with this compiler. However, you are right. I'm using the wrong compiler. Thank you for your help,

Tyler
 
Status
Not open for further replies.
Back