If Statements and String Values

I am using if statements for the user to type in a command and check w

Page 1 of 1

8 Replies - 1603 Views - Last Post: 04 November 2009 - 12:47 PM Rate Topic: -----

#1 cronus8992   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 29-October 09

If Statements and String Values

Posted 29 October 2009 - 07:46 AM

I have no access to a compiler at the momment so I am just wanting to check if this code will work. I now littl eabout strings at the momment.

I am getting a command from the user with getline and the string is declared as pick.

When they use a certain command it takes them to another void function depending on the value they set to the string.

So I have all the main stuff like....

#include<iostream>
#include<string>
using namespace std;

void MainMenu();
void NewGame();
void LoadGame();
void GameInfo();
void Credits();

string Pick;

int main()
{
MainMenu();
}


So that is my set up...

Then this is the code I want to make sure is correct.

void MainMenu()
{
   system("cls");
   cout << "\nNew Game" << endl;
   cout << "\nLoad Game" << endl;
   cout << "\nCredits" << endl;
   cout << "\nGame Info" << endl;
   cout << "\nExit" << endl;
   
   cout << "Type command:  ";
   getline(cin, Pick);
  
   if(Pick = "New Game")
	  NewGame();
   if(Pick = "Load Game")
	  LoadGame();
   if(Pick = "Credits")
	  Credits();
   if(Pick = "Game Info")
	  GameInfo();
   if(Pick = "Exit")
	  break;
   else
	  MainMenu();

}


I know they have to type in the string exactly to get it to call one of the functions, I am just making sure I havent missed anything in the code that would cause an error.

Thank you for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: If Statements and String Values

#2 teknique   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 11-April 06

Re: If Statements and String Values

Posted 29 October 2009 - 08:11 AM

To compare things you need to use a == instead of =.
Also it would probably be better to use if...else if...else structure.
   if(Pick == "New Game")
	  NewGame();
   else if(Pick == "Load Game")
	  LoadGame();
   else if(Pick == "Credits")
	  Credits();
   else if(Pick == "Game Info")
	  GameInfo();
   else if(Pick == "Exit")
	  break;
   else
	  MainMenu();


Was This Post Helpful? 1
  • +
  • -

#3 cronus8992   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 29-October 09

Re: If Statements and String Values

Posted 29 October 2009 - 08:13 AM

Ok thank you. I wasnt sure about the == on string values. Other than those the code seems to be fine?
Was This Post Helpful? 0
  • +
  • -

#4 teknique   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 11-April 06

Re: If Statements and String Values

Posted 29 October 2009 - 08:23 AM

The compare is case sensitive. So if I'm the user and I type in "New Game" it'll work but if I were to type in "new game" it wouldn't work.

This post has been edited by teknique: 29 October 2009 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 olibenu   User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: If Statements and String Values

Posted 29 October 2009 - 08:53 AM

it seems u want to know if ur getline will read a string with spaces, yes it wil ;)
Was This Post Helpful? 0
  • +
  • -

#6 cronus8992   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 29-October 09

Re: If Statements and String Values

Posted 30 October 2009 - 09:25 AM

Ok the I made this code so they could have more of a choice of what they type in....

void ItemCheck;
{
	  system("cls");
   if(Item == "bartenders note" && BartendersNote >= 1)
   {
	  cout << "\n\t*******************" << endl;
	  cout << "\n\t| Bartenders Note |" << endl;
	  cout << "\n\t*******************" << endl;
	  cout << "\n\nUse" << "\nDrop" << "\nExit" << endl;
	  cout << "\n\nType Commands:  ";
	  getline(cin, ItemCommand);
		 BartendersNoteCommands();
   } 
   else if(Item == "Bartenders Note" && BartendersNote >= 1)
   {
	  cout << "\n\t*******************" << endl;
	  cout << "\n\t| Bartenders Note |" << endl;
	  cout << "\n\t*******************" << endl;
	  cout << "\n\nUse" << "\nDrop" << "\nExit" << endl;
	  cout << "\n\nType Commands:  ";
	  getline(cin, ItemCommand);
		 BartendersNoteCommands();
   }
   else if(Item == "Bartenders note" && BartendersNote >= 1)
   {
	  cout << "\n\t*******************" << endl;
	  cout << "\n\t| Bartenders Note |" << endl;
	  cout << "\n\t*******************" << endl; 
	  cout << "\n\nUse" << "\nDrop" << "\nExit" << endl;
	  cout << "\n\nType Commands:  ";
	  getline(cin, ItemCommand);
		 BartendersNoteCommands();
   } 
   else if(Item == "bartenders Note" && BartendersNote >= 1)
   {
	  cout << "\n\t*******************" << endl;
	  cout << "\n\t| Bartenders Note |" << endl;
	  cout << "\n\t*******************" << endl;
	  cout << "\n\nUse" << "\nDrop" << "\nExit" << endl;
	  cout << "\n\nType Commands:  ";
	  getline(cin, ItemCommand);
		 BartendersNoteCommands();
   } 
} 



But this seems a little much....I was wanting to see if someone had an easier way....
Was This Post Helpful? 0
  • +
  • -

#7 teknique   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 11-April 06

Re: If Statements and String Values

Posted 30 October 2009 - 10:37 PM

You would probably want to convert the command that the user enters to all lower case and then you can just check that instead of trying to check all the possible combinations.

Using something like Snippet 39
Was This Post Helpful? 1
  • +
  • -

#8 cronus8992   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 29-October 09

Re: If Statements and String Values

Posted 02 November 2009 - 01:02 PM

Ok so something like this would be good?

#include<iostream>
#include<ctype>
#include<string>
using namespace std;

void ConvertToLower;
char Item[80];

int main()
{
   cout << "Type a string to convert: ";
   cin.getline(Item,79);
   ConvertToLower();
   
   cout << "\nThe string has been converted into: " << Item << endl;
   return 0;
}

void ConvertToLower()
{
   int i;
   int length = strlen(Item);

   for( i = 0; i < length; i++)
   Item[i] = tolower(Item, [i]);
}

This post has been edited by cronus8992: 03 November 2009 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

#9 cronus8992   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 29-October 09

Re: If Statements and String Values

Posted 04 November 2009 - 12:47 PM

bump

This post has been edited by cronus8992: 04 November 2009 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1