Validating a string???

6 replies [Last post]
dockab
Offline
Last seen: 3 years 43 weeks ago
Joined: 2010-03-05
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
Offline
Last seen: 4 years 36 weeks ago
Joined: 2009-11-02
bOt Points: 457
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, 2010-03-09 19:52.

dockab
Offline
Last seen: 3 years 43 weeks ago
Joined: 2010-03-05
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
Offline
Last seen: 4 years 36 weeks ago
Joined: 2009-11-02
bOt Points: 457
welcome!

 Its ok ..keep posting 

Happy coding 

dockab
Offline
Last seen: 3 years 43 weeks ago
Joined: 2010-03-05
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
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Your program is working fine

Hi dockab,

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

pointer problem

Founder and Admin

dockab
Offline
Last seen: 3 years 43 weeks ago
Joined: 2010-03-05
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.