Says I have error

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 331 Views - Last Post: 25 March 2012 - 11:43 AM Rate Topic: -----

#1 #include <iostream>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 12

Says I have error

Posted 25 March 2012 - 03:30 AM

I thought it was fine but bash says that number was not declared in this scope 50 times and that In function ‘int main() and it excepts ';' before cout

Here is the code :

#include <iostream>
using namespace std;
int main(void)
{
  int number
  
  cout<< "Hello Noah!\n"
  cout<< "Play a Game?\n"
  cout << "How about Chess?\n"
  cout << "One (1) if yes two (2) for no";
  
  int number 
	         cin (number);
		 
	    if (number==1);
       cout<< "What Game?\n"
       cout<< " three (3) for Chess\n"
       cout<< "four (4) Checkers\n"
       cout<< "five (5) for Battleship";
       if (number == 3)
	 cout<< "Check Mate!!";
	if (number == 4)
	  cout<< "I Win!";
	if (number == 5)
	  cout<< "I Sunk Your Ship!";
	if (number == 2)
	cout<< "Good Bye";
 return 0;
}



Could You figure out whats wrong ?

This post has been edited by no2pencil: 25 March 2012 - 03:31 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Says I have error

#2 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4500
  • View blog
  • Posts: 24,965
  • Joined: 10-May 07

Re: Says I have error

Posted 25 March 2012 - 03:32 AM

You don't have a semi-colon on your initialization of integer variable "number".

Also you initialize it twice. That wouldn't cause a compile error, but it could certainly cause a logic error.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: Says I have error

Posted 25 March 2012 - 03:34 AM

And it's cin >> number;.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4500
  • View blog
  • Posts: 24,965
  • Joined: 10-May 07

Re: Says I have error

Posted 25 March 2012 - 03:37 AM

This isn't a compile error, but will cause a logic error :

Quote

  if (number==1);

Your conventional if statement will check if number is equal to zero, & then the semi-colon states to end the execution. No action will be taken, regardless if it is or is not equal to one.
Was This Post Helpful? 1
  • +
  • -

#5 #include <iostream>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 12

Re: Says I have error

Posted 25 March 2012 - 03:42 AM

Is that all!

Thanks that was for my little brother
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4500
  • View blog
  • Posts: 24,965
  • Joined: 10-May 07

Re: Says I have error

Posted 25 March 2012 - 03:45 AM

Glad that worked out for you.
Was This Post Helpful? 0
  • +
  • -

#7 #include <iostream>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 12

Re: Says I have error

Posted 25 March 2012 - 03:46 AM

What about the int main(void) error what do I do about that
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: Says I have error

Posted 25 March 2012 - 03:46 AM

In the future, when you compile and get errors, you need to copy and paste the errors exactly as they appear. If the program compiles but does not give you the output you expect, then you need to tell us what output you expect, what you received as output instead, and any input you may have provided to the program.
Was This Post Helpful? 0
  • +
  • -

#9 #include <iostream>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 12

Re: Says I have error

Posted 25 March 2012 - 03:49 AM

code.cpp: In function ‘int main()’:
code.cpp:8:3: error: expected ‘;’ before ‘cout’
code.cpp:12:7: error: redeclaration of ‘int number’
code.cpp:5:7: error: ‘int number’ previously declared here
code.cpp:17:8: error: expected ‘;’ before ‘cout’

Thats what bash says
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4500
  • View blog
  • Posts: 24,965
  • Joined: 10-May 07

Re: Says I have error

Posted 25 March 2012 - 03:51 AM

View Post#include <iostream>, on 25 March 2012 - 06:46 AM, said:

What about the int main(void) error what do I do about that

Quote

In function ‘int main() and it excepts ';'

You have an error in function main. We pointed out to where the errors are in that function.

View Post#include <iostream>, on 25 March 2012 - 06:49 AM, said:

code.cpp: In function ‘int main()’:
code.cpp:8:3: error: expected ‘;’ before ‘cout’
code.cpp:12:7: error: redeclaration of ‘int number’
code.cpp:5:7: error: ‘int number’ previously declared here
code.cpp:17:8: error: expected ‘;’ before ‘cout’

Thats what bash says


Lines 7 & 8 :

Quote

  cout<< "Hello Noah!\n"
  cout<< "Play a Game?\n"




Error :

Quote

error: expected ‘;’

Put a semi-colon at the end of your instructions.

As per cout example :

cout << "Output sentence"; // prints Output sentence on screen
cout << 120;               // prints number 120 on screen
cout << x;                 // prints the content of x on screen 

Was This Post Helpful? 0
  • +
  • -

#11 #include <iostream>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 12

Re: Says I have error

Posted 25 March 2012 - 03:51 AM

Oh here is current code:
#include <iostream>
using namespace std;
int main(void)
{
  int number;;
  
  cout<< "Hello Noah!\n"
  cout<< "Play a Game?\n"
  cout << "How about Chess?\n"
  cout << "One (1) if yes two (2) for no";
  
  int number ;;
	         cin >> number;
		 
	    if (number==1)
       cout<< "What Game?\n"
       cout<< " three (3) for Chess\n"
       cout<< "four (4) Checkers\n"
       cout<< "five (5) for Battleship";
       if (number==3)
	 cout<< "Check Mate!!";
	if (number==4)
	  cout<< "I Win!";
	if (number==5)
	  cout<< "I Sunk Your Ship!";
	if (number==2)
	cout<< "Good Bye";
 return 0;
}

Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4500
  • View blog
  • Posts: 24,965
  • Joined: 10-May 07

Re: Says I have error

Posted 25 March 2012 - 03:53 AM

Fails:
  cout<< "Hello Noah!\n"
  cout<< "Play a Game?\n"
  cout << "How about Chess?\n"



Works:
  cout << "One (1) if yes two (2) for no";
	cout<< "Good Bye";




Notice the difference?
Was This Post Helpful? 0
  • +
  • -

#13 #include <iostream>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 12

Re: Says I have error

Posted 25 March 2012 - 03:58 AM

so like this ?
#include <iostream>
using namespace std;
int cout << "One (1) if yes two (2) for no";
2	  cout<< "Good Bye";main(void)
{
  int number;;
  cout<< "Hello Noah Play a game?"
  cout << "One (1) if yes two (2) for no";
	  cout<< "Good Bye";
  cout<< "Play a Game?\n"
  cout << "How about Chess?\n"
  cout << "One (1) if yes two (2) for no";
  
  int number ;;
	         cin >> number;
		 
	    if (number==1)
       cout<< "What Game?\n"
       cout<< " three (3) for Chess\n"
       cout<< "four (4) Checkers\n"
       cout<< "five (5) for Battleship";
       if (number==3)
	 cout<< "Check Mate!!";
	if (number==4)
	  cout<< "I Win!";
	if (number==5)
	  cout<< "I Sunk Your Ship!";
	if (number==2)
	cout<< "Good Bye";
 return 0;
}

Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4500
  • View blog
  • Posts: 24,965
  • Joined: 10-May 07

Re: Says I have error

Posted 25 March 2012 - 04:01 AM

Why would you change main from int to void? & no, you still have cout lines that you did not end with the semi-colon.

For example :

Quote

       cout<< "four (4) Checkers\n"
       cout<< "five (5) for Battleship";

Why did you provide a semi-colon to the end of the 2nd set of lines, but not the first?
Was This Post Helpful? 0
  • +
  • -

#15 #include <iostream>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 12

Re: Says I have error

Posted 25 March 2012 - 04:11 AM

#include <iostream>
using namespace std;
int main()
{
  int number;;
  cout<< "Hello Noah Play a game?";
  cout << "One (1) if yes two (2) for no";
	  cout<< "Good Bye";
  cout<< "Play a Game?\n";
  cout << "How about Chess?\n";
  cout << "One (1) if yes two (2) for no";
  
  int number ;;
	         cin >> number;
		 
	    if (number==1)
       cout<< "What Game?\n";
       cout<< " three (3) for Chess\n";
       cout<< "four (4) Checkers\n";
       cout<< "five (5) for Battleship";
       if (number==3)
	 cout<< "Check Mate!!";
	if (number==4)
	  cout<< "I Win!";
	if (number==5)
	  cout<< "I Sunk Your Ship!";
	if (number==2)
	cout<< "Good Bye";
 return 0;
}



errors :
code.cpp:3:10: error: expected initializer before ‘<<’ token
code.cpp:4:1: error: expected unqualified-id before numeric constant
code.cpp: In function ‘int main()’:
code.cpp:14:7: error: redeclaration of ‘int number’
code.cpp:6:7: error: ‘int number’ previously declared here
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2