7 Replies - 823 Views - Last Post: 27 May 2012 - 08:42 PM Rate Topic: -----

#1 trh51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-May 12

Having problems with C++ code: "else if " statements I think.

Posted 22 May 2012 - 04:52 PM

I am using g++ compiler in a Linux environment. The main problem is that it is NOT working the way it should be. I am having problems right about where it ask for the command.

Here is the code:

#include <iostream>

using namespace std;

void intro()
{
	cout << "Welcome to CASTLE" << endl;
	cin.get();
	cout << "A midevil RPG. Source is avaliable at thetrh51.blogspot.com along with documentation." << endl;
	cout << "All the commands, descriptions are avaliable at thetrh51.blogspot.com";
	cin.get();
}

int menu()
{
	cout << "please visit thetrh51.blogspot.com for the source and info on the program including commands." << endl;
	int dec;
	cout << "1. save" << endl << "2. load" << endl << "3. contiue" << endl << "4. exit" << endl;
	cout << "Please select from above: ";
	cin >> dec;
}

void save()
{
	
}

char getinfo()
{
	cout << "Please enter your characters name [max 12 characters]: ";
	char playername[12];
	cin >> playername;
	cout << endl <<  "OK " << playername << " how old do you want to be? [20-90]: ";
	int playerage;
	cin >> playerage;
	cout << endl << playername << " what is your difficulty going to be? [1-10]: ";
	int difficulty;
	cin >> difficulty;
	cout << endl <<  "Here is what you entered" << endl;
	cout << "Your name is " << playername << endl;
	cout << "Your age is " << playerage << endl;
	cout << "Your dificulty level is " << difficulty << endl;
}

int main()
{
	int score;
	licence();
	intro();
	getinfo();
	char command[12];
	cout << "command: ";
	cin >> command;
	if (command == "menu")
	{
		menu();
		cin.get();
	}
	else if (command == "help")
	{
		cout << "visit thetrh51.blogspot.com  for more info.";
		cin.get();
	}
	else if (command == "exit")
	{
		return 1;
	}
}



PS. If any one wants to help me with the save file it would be appreciated.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Having problems with C++ code: "else if " statements I think.

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Having problems with C++ code: "else if " statements I think.

Posted 22 May 2012 - 05:42 PM

Why did you define menu() as type int, & yet it has no return value?

Same with getinfo(), you define it as char, yet you don't return any value.

Your problem is the way in which you are comparing strings :

Quote

	char command[12];


You define an array, command, & it has 12 elements. You don't initialize them, so they most likely contain garbage.


Quote

	cout << "command: ";
	cin >> command;


You then read in the user input, presumably less than 12 characters.

Quote

	if (command == "menu")


You then check a 4 element array string value to the 12 possible elements.

My suggestion would be to use string libraries, & do a proper comparison. Not integer based conditional if statements.
Was This Post Helpful? 1
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Having problems with C++ code: "else if " statements I think.

Posted 22 May 2012 - 05:44 PM

Quote

I am having problems right about where it ask for the command.


What problems are you having exactly? The more concisely you can define the problem, the better we can help.
Was This Post Helpful? 0
  • +
  • -

#4 trh51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-May 12

Re: Having problems with C++ code: "else if " statements I think.

Posted 22 May 2012 - 09:28 PM

I mean when I run the program when you give it input at the command promt nothing happens. Thanks no to pencil for your help. I realize my mistake now. Thanks for helping!
Was This Post Helpful? 0
  • +
  • -

#5 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Having problems with C++ code: "else if " statements I think.

Posted 22 May 2012 - 10:09 PM

As far as the save function is concerned, how about reading this link ??
Was This Post Helpful? 0
  • +
  • -

#6 trh51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-May 12

Re: Having problems with C++ code: "else if " statements I think.

Posted 23 May 2012 - 06:09 PM

Thanks you guys for helping someone new with his code. no2pencil, your solution worked and thanks for helping me with my mistakes.

Much love,
Tristan!
Was This Post Helpful? 0
  • +
  • -

#7 trh51  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-May 12

Re: Having problems with C++ code: "else if " statements I think.

Posted 27 May 2012 - 08:39 PM

So I took your advice to heart and rewrote my code! Thanks

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>

using namespace std;

void licence(){
        cout << "\n\
        This file is part of Tristan'sOS\n\
        \n\
        This program is free software: you can redistribute it and/or modify\n\
        it under the terms of the GNU General Public License as published by\n\
        the Free Software Foundation, either version 3 of the License, or\n\
        (at your option) any later version.\n\
        \n\
        This program is distributed in the hope that it will be useful,\n\
        but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
        GNU General Public License for more details.\n\
        You should have received a copy of the GNU General Public License\n\
        along with this program.  If not, see <http://www.gnu.org/licenses/>." << endl;
    }

void calc(){
        system("cls");
        int a, b, c;
        float x, y, z;
        int choi;
        int count;
        count = 1;
        while (count == 1){
            cout << endl << "Welcome to Tristan'sOS calculator\n";
            cout << "\n\
            Please give some input! \n\
            1. Multiplication\n\
            2. Division \n\
            3. Addition \n\
            4. Subtraction \n\
            5. Powers \n\
            6. Square root\n\
            7. Trig functions [in progress]\n\
            99. Exit \n\
            \n\
            Choice: ";
            cin >> choi;
            if (choi == 1){
                system("cls");
                cout << "Please input a 2 numbers to multiply: ";
                cin >> x >> y;
                z = x * y;
                cout << endl << "Your anser is " << z << endl;
            }
            else if (choi == 2){
                system("cls");
                cout << "The formula is A / B = C";
                cout << endl << "Please enter A then B: ";
                cin >> a >> b;
                z = a / b;
                cout << endl << "Your anser is " << z << " [if you got a wierd anser then switch A and B]" << endl;
            }
            else if (choi == 3){
                system("cls");
                cout << "Please enter the numbers you want to add: ";
                cin >> a >> b;
                z = x + y;
                cout << endl << "Your anser is " << z;
            }
            else if (choi == 99){
                system("cls");
                count = 0;
            }
            else if (choi == 4){
                system("cls");
                cout << endl << "The formula is A - B = C. Please enter A then B: ";
                cin >> x >> y;
                z = x-y;
                cout << "The anser is " << z << "[if the anser is wierd then try flipping A and B]" << endl;
            }
            else if (choi == 5){
                system("cls");
                cout << endl << "What exponent do you want to use? ";
                double exponent;
                cin >> exponent;
                cout << endl << "what number do you want to use?" ;
                double num;
                cin >> num;
                double anser;
                anser = pow (num, exponent);
                cout << endl << "Your anser is " << anser << endl;
            }
            else if (choi == 6){
                system("cls");
                cout << "What number you want to square root? ";
                double number;
                double ansersqrt;
                cin >> number;
                ansersqrt = sqrt(number);
                cout << endl << "The anser is " << ansersqrt << endl;
            }
            else if (choi == 7){
                system("cls");
                cout << "Still in progress :)/> press any key then enter to continue";
                char derka;
                cin >> derka;
                system("cls");
            }
        }
}

int main()
{
    int i = 1;
    cout << "Welcome to Tristan'sOS. Documentation can be found at thetrh51.blogspot.com" << endl;
    while (i==1){
        char com[10];
        cout << endl << "command :";
        cin >> com;
        cout << endl;
        if (strcmp(com, "help") == 0){
            cout << "the only commands so far are licence, help, calc, and exit" << endl;
        }
        else if (strcmp(com, "exit") == 0){
            cout << "now exiting" << endl;
            i = 0;
        }
        else if (strcmp(com, "licence") == 0){
            licence();
        }
        else if (strcmp(com, "calc") == 0){
            calc();
        }
    }
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Having problems with C++ code: "else if " statements I think.

Posted 27 May 2012 - 08:42 PM

Since you are using integers, rather than doing all of the if/else you could clean up your code using a switch case.

Glad to see you are making process, & thank you for updating!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1