Validating a string???

6 replies [Last post]
dockab
User offline. Last seen 15 weeks 3 days ago. Offline
Joined: 03/05/2010
bOt Points: 66

Good Morning All,

I am sure this is something simple but I'd like some help. In the example code below I would like to evaluate a string for a response or group of responses using any of the loop functions.

Any help would be greatly appreciated.

Keith

#include <stdio.h>
#include <iostream.h>
using namespace std;

string userName;
string gameOn;

int main()
{
cout << "What is your name? \n";
cin >> userName;
cout << "Nice to meet you " << userName << "!\n\n";

cout << "Would you like to play a game?: \n";
cin >> gameOn;
    if ????gameOn????  //an answer of "Yes", "yes", "Y" or "y" is the criteria
        {
        cout << "Great, lets begin!\n\n";
        }

return 0;
}

aamiraarfi
aamiraarfi's picture
User offline. Last seen 1 day 47 min ago. Offline
Joined: 11/02/2009
bOt Points: 417
Hi! try this on botskool online c compiler

Hi! docklab try this, hope it works for you 

#include <stdio.h>

#include <iostream.h>

using namespace std;

string userName;

string gameOn;

int main()

{

cout << "What is your name? \n";

cin >> userName;

cout << "Nice to meet you " << userName << "!\n\n";

cout << "Would you like to play a game?: \n";

cin >> gameOn;

if ((gameOn=="Yes")||(gameOn=="yes")||(gameOn=="Y")||(gameOn=="y"))

        {

        cout << "Great, lets begin!\n\n";

        }

return 0;

}

c compiler input

Try this on bOtskOOl online c/c++ compiler

Note:- This post has been edited by aamiraarfi at Tue, 03/09/2010 - 14:22.

dockab
User offline. Last seen 15 weeks 3 days ago. Offline
Joined: 03/05/2010
bOt Points: 66
It is always so simple once

It is always so simple once you see it. Thank you my friend!!!

Doc

aamiraarfi
aamiraarfi's picture
User offline. Last seen 1 day 47 min ago. Offline
Joined: 11/02/2009
bOt Points: 417
welcome!

 Its ok ..keep posting 

Happy coding 

dockab
User offline. Last seen 15 weeks 3 days ago. Offline
Joined: 03/05/2010
bOt Points: 66
 Im back. I am just playing

 Im back. I am just playing around and trying to learn C++ but I'm now stuck with a pointer problem.

Below is a continuation of the previous code, and while it compiles fine I get this error with any input:

 

////////////////////////////////////////////

PracticeCPPApp(2331) malloc: *** error for object 0x100005220: pointer being freed was not allocated*** set a breakpoint in malloc_error_break to debug 
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all

/////////////////////////////////////////////

The full sample code is below. If you can help me understand this error, I would greatly appreciate it.

 

#include <iostream>
#include <string>
 
using namespace std;
 
string userName;
string gameOn;
string playAgain;
 
int startGame();
 
int main()
{
cout << "What is your name? \n";
cin >> userName;
cout << "Nice to meet you!\nWould you like to play a game?: \n";
cin >> gameOn;
    if ((gameOn=="Yes")||(gameOn=="yes")||(gameOn=="Y")||(gameOn=="y"));
{
        cout << "Great, lets begin!\n\n";
        startGame();
}
cout << "Fun! Would you like to play once more? \n";
cin >> playAgain;
if ((playAgain=="Yes")||(playAgain=="yes")||(playAgain=="y")||(playAgain=="Y"))
        {
cout << "Ok!\n";
startGame();
        }   
cout << "Good bye!\n";
return 0;
}
 
//--------------------------------
int startGame()
{
int userNum;
int myNum;
myNum=3;
cout << "Choose a number between 1 and 10: \n";
cin >> userNum;
do 
{
cout << "Nope, choose another number! \n";
cin >> userNum;}
while (userNum!=myNum);
cout << "Good Guess!\n";
 
return 0;
}
 

 

 

 

shashwat
User offline. Last seen 6 hours 39 min ago. Offline
Joined: 02/18/2009
bOt Points: 1056
Your program is working fine

Hi dockab,

Your program is working fine in bOtskOOl Online Compiler. Check the screenshots below -

pointer problem

dockab
User offline. Last seen 15 weeks 3 days ago. Offline
Joined: 03/05/2010
bOt Points: 66
Correct Sashwat, It does

Correct Sashwat, It does indeed work with in this compiler, 

With in Xcode however, the code compiles without error but fails to run and results in the pointer error.