LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2013, 03:45 PM   #1
Djyou
LQ Newbie
 
Registered: Jun 2013
Distribution: Whatever works for the job.
Posts: 25

Rep: Reputation: Disabled
else without preceding if error.


So I'm working on a very basic text adventure game. I know that if else pyramids are frowned upon in programmes, though that isn't the issue here. I am looking for either a way to resolve the problem or a different way to go at the solution. So if the current method can't be fixed or it's easier to go at it a different way I'm open to either solution. Though I would prefer an explanation of why the solution works whatever it is. This is in C++
Enough intro on to the code and the error log.

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
int main(){
	std::string command;
	while( 1 == 1 ){
		std::cout << "Input Command" << std::endl;
		getline(std::cin, command);
		if( command == "exit"){
			std::cout << "Exit command accepted" << std::endl;
		}
		else{
			if(command == "; DROP TABLE students;"){
				std::cout << "Haha, very funny" << std::endl;
			}
		}
		else{
			if(command == "Ascend"){
			std::cout << "You have Ascended to godhood" << std::endl;
			std::cout << "Victory is your's"
			std::cout << "Just type 'cow' to end the game" << std::endl;
			getline(std::cin, command)
			if(command == "cow"){
				exit(0);
			}
			else{
				std::cout << "Bad Player Bad" << std::endl;
			}
		}
		else{
			std::cout << "Command not recognized" << std::endl;
		}
	}
return 0;
}
Logs

Code:
Source.cpp: In function ‘int main()’:
Source.cpp:17:3: error: ‘else’ without a previous ‘if’
   else{
   ^
Source.cpp:21:4: error: expected ‘;’ before ‘std’
    std::cout << "Just type 'cow' to end the game" << std::endl;
    ^
Source.cpp:23:4: error: expected ‘;’ before ‘if’
    if(command == "cow"){
    ^
Source.cpp:26:4: error: expected ‘}’ before ‘else’
    else{
    ^
Source.cpp:30:3: error: ‘else’ without a previous ‘if’
   else{
   ^
Details, I am on lubuntu using g++ from the repositories.

Any help would be appreciated.

--Djyou

Last edited by Djyou; 11-03-2013 at 03:55 PM. Reason: Forgot to include language
 
Old 11-03-2013, 04:09 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
I've done very little with C++, but shouldn't it go like this:

Code:
		if( command == "exit"){
			std::cout << "Exit command accepted" << std::endl;
		} 
                else if(command == "; DROP TABLE students;") {
				std::cout << "Haha, very funny" << std::endl;
		} 
                else if(command == "Ascend") {
			std::cout << "You have Ascended to godhood" << std::endl;
			std::cout << "Victory is your's";
			std::cout << "Just type 'cow' to end the game" << std::endl;
			getline(std::cin, command);
			if(command == "cow"){
				exit(0);
			}
			else{
				std::cout << "Bad Player Bad" << std::endl;
			}
		} 
                else {
			std::cout << "Command not recognized" << std::endl;
		}
?

i.e.:

Code:
if(condition1) {
   // condition1 is true - ignore everything else
} else if(condition2) {
   // no braces between the else and the if,
   // part of the if, else if, else chain
   if(condition3) {
      // only evaluated if condition2 is true
      // not part of the chain
   }
} else {
   //neither condition1 or condition2 match 
}

Last edited by Robhogg; 11-03-2013 at 04:19 PM.
 
1 members found this post helpful.
Old 11-03-2013, 04:16 PM   #3
Djyou
LQ Newbie
 
Registered: Jun 2013
Distribution: Whatever works for the job.
Posts: 25

Original Poster
Rep: Reputation: Disabled
Thanks, that gave me hint I need there were a couple other syntax errors but those were solved easily enough.
Here's the finished code in case anyone reading this later wants it.

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
int main(){
	std::string command;
	while( 1 == 1 ){
		std::cout << "Input Command" << std::endl;
		getline(std::cin, command);
		if( command == "exit"){
			std::cout << "Exit command accepted" << std::endl;
		}
		else if(command == "; DROP TABLE students;"){
			std::cout << "Haha, very funny" << std::endl;
		}
		else if(command == "Ascend"){
		std::cout << "You have Ascended to godhood" << std::endl;
		std::cout << "Victory is your's" << std::endl;
		std::cout << "Just type 'cow' to end the game" << std::endl;
		getline(std::cin, command);
			if(command == "cow"){
				exit(0);
			}
			else{
				std::cout << "Bad Player Bad" << std::endl;
			}
		}
		else{
			std::cout << "Command not recognized" << std::endl;
		}
	}
return 0;
}
Thanks again, marking as solved.

--Djyou
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
backslash preceding a shell command kurtcpp Linux - General 2 03-22-2011 04:05 AM
[SOLVED] Need help using sed to remove preceding and trailing spaces in CSV netwaves Programming 11 09-20-2010 07:14 AM
mozilla:tab in background (personal settings take preceding?) tommeke Linux - Software 1 04-07-2005 11:05 AM
Apache directory preceding domain in url MasterC Linux - Networking 9 03-14-2005 06:32 AM
Removing a preceding pattern from each sentence ganninu Programming 2 12-11-2003 07:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration