View Full Version : C++ Code Problem
Gladiat0r
10-17-2004, 03:14 PM
Yea I'm just starting to learn C++ and I've run into a "sytax error" in my code and I can't figure out what it is... if anyone has any ideas tell me.
This is the code
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string code;
string STUPED;
string today;
int ID;
cout << "Enter your name: ";
cin >> name;
if (name == "Nate")
cout << "Hey Nate! You are the creator of this code!";
cin >> code;
if (name == "Sam")
cout << "Hey Sam! You are STUPED!";
cin >> STUPED;
else
cout << "Hello " << name << " how are you today?";
cin >> today;
return 0;
}
Where it says 'else' is where it says I have a syntax error
Panopticon
10-17-2004, 03:34 PM
you need {} around your if...else block. You may only omit them if your following code is only one line and there isn't a variable definition. Try this out see if it works. Also you may want to use a switch, IMO it clearifies things up a bit, and when compiled to machine code its the same as an if block anyways.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string code;
string STUPED;
string today;
int ID;
cout << "Enter your name: ";
cin >> name;
if (name == "Nate")
{
cout << "Hey Nate! You are the creator of this code!";
cin >> code;
}
elseif (name == "Sam")
{
cout << "Hey Sam! You are STUPED!";
cin >> STUPED;
}
else
{
cout << "Hello " << name << " how are you today?";
cin >> today;
}
return 0;
}
Gladiat0r
10-17-2004, 03:40 PM
Thanks a lot!!!
It works! :) :)
Now time to figure out what to do with it
Panopticon
10-17-2004, 03:46 PM
just learning eh? Try out things like functions, a looping menu structure...(like press 1 to do this, 2 to do that, 3 to exit etc....) when you get comfortable with that you can do things like class objects, recursion, polymorphism, overloading functions, overloading operaters, virtual functions, derived/base class.....and it just keeps going!.
BlinkingViki
10-17-2004, 04:28 PM
i love loops ^^ I once made and awesome game with a clock mechanism that used loops :D It was beautiful...But my school didn't have a visual compiler...so it was all text only, and all. images had to be ASCII...it was interesting :)
Maybe you should make it into an RPG where the character's anem is the name you input. Then you'll have some nice, long, complicated code. A friend of mine once made an adventure where you had to wake up and make yourself breakfast ^^ It was kinda neat.
dying to live
10-17-2004, 04:31 PM
does any one know about turing and are you good at it, if yes then can you pm me please?????
Panopticon
10-17-2004, 04:50 PM
i love loops ^^ I once made and awesome game with a clock mechanism that used loops :D It was beautiful...But my school didn't have a visual compiler...so it was all text only, and all. images had to be ASCII...it was interesting :)
Maybe you should make it into an RPG where the character's anem is the name you input. Then you'll have some nice, long, complicated code. A friend of mine once made an adventure where you had to wake up and make yourself breakfast ^^ It was kinda neat.
a visual compiler?!?!? thats a new one to me.
Ryan2003
10-18-2004, 04:33 PM
bah data structures are more fun..... lol.... this project reminds me of the hello world project. I guess ya gotta start somewhere, although im still dumbfounded by the word "STUPED" referring to someone else yet it is spelled wrong? lol.... anyways nice job on the correcting..... those damn if elses can be tricky and easy to miss simple mistakes like that. In many cases they are the hardest.
MrDeliquent85
10-18-2004, 05:10 PM
I am interested in learning C++, I know many places to learn it but i am looking for a good free compiler. Any one know where ?
born2lose
10-18-2004, 06:39 PM
Are you guys learning programming in high school now?
Panopticon
10-18-2004, 08:00 PM
I have a 3yr community college diploma for "Computer Programmer/Analyst"
I have about 5yrs exp with C++ and I'm still a newbie in my eyes.
As for a free compiler. well for games I like to use Dev-C++(its an IDE) and the windows port of the gc++ compiler. You could also use Borland 5.5 with Dev-C++. For windows and general RAD apps I have Visual Studio.NET 2003 However that wasn't free :S
Ryan2003
10-20-2004, 01:39 AM
I have a 3yr community college diploma for "Computer Programmer/Analyst"
I have about 5yrs exp with C++ and I'm still a newbie in my eyes.
As for a free compiler. well for games I like to use Dev-C++(its an IDE) and the windows port of the gc++ compiler. You could also use Borland 5.5 with Dev-C++. For windows and general RAD apps I have Visual Studio.NET 2003 However that wasn't free :S
Very good choice. Dev is the best free compiler out there(opinion of course) in which our College uses(MTV was here today!) Kirkwood Community College. I believe many if not most Universities use it as well.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.