|
 |
|
 |
07-18-2005, 10:29 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Dec 2004
Posts: 43
|
Parse Error with "else" in simple game.
Alright... to let everyone know, even though I have spent some months studying programming. I'm not down with the linguo. Nor do I have as high as a vocabulary as valmont. So please, keep the vocab and linguo down a bit.
Alright the question. I am getting a parse error on my little game I am creating. One part where there is an if statement, I try to put an else statement in the code block immediatly after the if statement, when I compile it, it shows a parse error for the line with the else code. This is the actual code...
Code:
if (FamAnsw == Answ)
{
cout << "CORREECTT!!!/n";
cout << "[Applause] [Bells] [Whistles] [explosions]/n";
else
cout << "ERRR! Wrong! Sry play again!/n";
}
Heres the the full code if anyone wants to read it...
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
char Fam;
char FamAnsw;
char Answ[] = "Franz Ferdinand";
cout << "/tWelcome To Family Fued George style/n";
cout << "Your host George Tuosto! [applause]/n";
cout << "Our family today is...\n";
cout << "Enter your name here: ";
cin >> Fam;
cout << "The " << Fam << " family!\n";
cout << "Here is your first question... ";
cout << "... Who was assassinated in 1914 that started WW1?/n";
cout << "Answer: ";
cin >> FamAnsw;
if (FamAnsw == Answ)
{
cout << "CORREECTT!!!/n";
cout << "[Applause] [Bells] [Whistles] [explosions]/n";
else
cout << "ERRR! Wrong! Sry play again!/n";
}
cout << "Thats it for todays show of Family Fued George style!\n";
return 0;
}
If someone could tell me why it outputs a parse error with the else code, plz tell me. Thanx in advance!
PS: By the way, just to let any newbies know... the book C++ for dummies, though not too hard to understand the chapters... don't go into depth with what parse errors or any usual error for that matter. Also doesn't give too many quizes on making your own programs, which would have help tremendosly. Pick up something else and stay away from this book.
Last edited by redhead; 07-18-2005 at 12:02 PM.
|
|
|
07-18-2005, 12:04 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
|
Code:
if (FamAnsw == Answ)
{
cout << "CORREECTT!!!/n";
cout << "[Applause] [Bells] [Whistles] [explosions]/n";
else
cout << "ERRR! Wrong! Sry play again!/n";
}
Just move the ending } up above teh else clause, since that tells where the if scope ends, and since the else is encapsulated in it, the compiler dosn't know when to use the else scope, so the code will be:
Code:
if (FamAnsw == Answ)
{
cout << "CORREECTT!!!/n";
cout << "[Applause] [Bells] [Whistles] [explosions]/n";
}
else
cout << "ERRR! Wrong! Sry play again!/n";
You might want to change FamAnsw and Answ to type std::string, since you can't store an entire string in a char. It has to either be a char* or a string, and when using char* you need to know how much memory it will take, where string will dynamicaly allocate teh memory needed, it is easier to use, pluss it has a buildin == operator, where char* dosnt, so you'd have to use strcmp() on that.
Oh, and your line ending, with /n should be \n, or std::endl, aswell with your /t it should be \t if you were trying to make a <tab> space.
|
|
|
07-18-2005, 02:24 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Dec 2004
Posts: 43
|
ok after changing all of my variables to string (since they only hold characters) I get this error instead.
anyone@george's:~$ g++ FamilyFued.cpp -o FamilyFued
FamilyFued.cpp: In function `int main(int, char**)':
FamilyFued.cpp:26: error: no match for 'operator==' in 'FamAnsw == Answ'
anyone@george's:~$
|
|
|
07-19-2005, 12:43 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
|
Huh??
Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
string Fam;
string FamAnsw;
string Answ("Franz Ferdinand");
cout << "\tWelcome To Family Fued George style\n";
cout << "Your host George Tuosto! [applause]\n";
cout << "Our family today is...\n";
cout << "Enter your name here: ";
getline(cin, Fam);
cout << "The " << Fam << " family!\n";
cout << "Here is your first question... ";
cout << "... Who was assassinated in 1914 that started WW1?\n";
cout << "Answer: ";
getline(cin, FamAnsw);
if (FamAnsw == Answ)
{
cout << "CORREECTT!!!\n";
cout << "[Applause] [Bells] [Whistles] [explosions]\n";
}
else
cout << "ERRR! Wrong! Sry play again!\n";
cout << "Thats it for todays show of Family Fued George style!\n";
return 0;
}
Quote:
bouncer@smaug{118} ~> g++ -Wall -o fab test.cpp
bouncer@smaug{119} ~> ./fab
Welcome To Family Fued George style
Your host George Tuosto! [applause]
Our family today is...
Enter your name here: Ib
The Ib family!
Here is your first question... ... Who was assassinated in 1914 that started WW1?
Answer: Franz Ferdinand
CORREECTT!!!
[Applause] [Bells] [Whistles] [explosions]
Thats it for todays show of Family Fued George style!
bouncer@smaug{120} ~>
|
Seems to me theres no error n it, altho I had to change the cin >> in order to catch anyone placing a <space> in the input.
|
|
|
07-19-2005, 04:10 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Dec 2004
Posts: 43
|
Why do I have to change cin? Whats the difference, other then the space, does cin >>, against getline(cin, FamAnsw)? Thanx in advance. O ya, I fixed it, I forgot how, but I did.
|
|
|
07-19-2005, 10:33 PM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
|
Considder this piece of code:
Code:
#include <iostream>
#include <string>
void reset_istream(void);
int main()
{
std::string user_input;
std::cout << "Please input a line with spaces in it: ";
std::cout.flush();
std::cin >> user_input;
std::cout << "cin >> input was: " << user_input << std::endl;
std::cout << "Remainder in istream is: ";
while('\n' != std::cin.peek())
{
std::cin >> user_input;
std::cout << user_input << " : ";
}
std::cout << std::endl;
reset_istream();
std::cout << "Please input a line with spaces in it: ";
std::cout.flush();
getline(std::cin, user_input);
std::cout << "getline() input was: " << user_input << std::endl;
return 0;
}
void reset_istream(void)
{
if(std::cin.eof())
std::cin.clear();
else
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
The difference is in the standard reading of istream with the std::istream >> std::string operator and the getline(std::string) function. The description of the getline() function kinda describes it:
Quote:
...
The getline() function is used with input streams, and reads characters into buffer until either: - num - 1 characters have been read,
- a newline is encountered,
- an EOF is encountered,
...
|
Last edited by redhead; 07-19-2005 at 11:03 PM.
|
|
|
07-20-2005, 06:27 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Dec 2004
Posts: 43
|
Ok, I believe I understand what getline does. It just reads everything on the line, and puts it into whatever variable it was required to be put into. I don't fully understand what all the std::'s are... I haven't reached anything like that in my C++ book ... yet maybe. Could you explain? If you have already explained, and I wasn't smart enough to capture it, could you tell me?
|
|
|
07-20-2005, 08:13 AM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
|
Quote:
|
I don't fully understand what all the std::'s are... I haven't reached anything like that in my C++ book
|
This is to claim the use of the std namespace, if I were to write alot of code I would just add a line at the top:
Code:
using namespace std;
or perhaps more specific:
Code:
using std::cout;
using std::cin;
using std::string;
to specify that when ever I am using something of type string it is the std namespace of string I am refering to, same with cout and cin.
This was what Valmont were talking about in the first thread where he said you'd have to implement it in your own namespace.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:58 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|