Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-18-2005, 10:29 AM   #1 (permalink)
slashdot
Registered User
 
Join Date: Dec 2004
Posts: 43
slashdot is on a distinguished road
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.
slashdot is offline   Reply With Quote
Old 07-18-2005, 12:04 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-18-2005, 02:24 PM   #3 (permalink)
slashdot
Registered User
 
Join Date: Dec 2004
Posts: 43
slashdot is on a distinguished road
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:~$
slashdot is offline   Reply With Quote
Old 07-19-2005, 12:43 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-19-2005, 04:10 PM   #5 (permalink)
slashdot
Registered User
 
Join Date: Dec 2004
Posts: 43
slashdot is on a distinguished road
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.
slashdot is offline   Reply With Quote
Old 07-19-2005, 10:33 PM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
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,
...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 07-19-2005 at 11:03 PM.
redhead is offline   Reply With Quote
Old 07-20-2005, 06:27 AM   #7 (permalink)
slashdot
Registered User
 
Join Date: Dec 2004
Posts: 43
slashdot is on a distinguished road
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?
slashdot is offline   Reply With Quote
Old 07-20-2005, 08:13 AM   #8 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where to get information on game programing online for linux. slashdot Platform/API C++ 0 07-10-2005 12:02 PM
Simple 2d game Nielszz Platform/API C++ 1 04-23-2005 11:22 AM
A simple Q fuzzybunny3 Platform/API C++ 1 10-29-2004 03:00 AM


All times are GMT -8. The time now is 08:14 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting