if statement problem

can someone please advice me on this if statement prob.

Page 1 of 1

8 Replies - 892 Views - Last Post: 25 March 2009 - 10:22 AM Rate Topic: -----

#1 Magnet  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 49
  • Joined: 13-October 08

if statement problem

Post icon  Posted 25 March 2009 - 08:57 AM

this is a section of my code

L.Display();
								cout<<"\n===========================================================";
								cout<<"\n\nAre You Sure You Want To Save This Information!? [Y/N]: ";
								cin>>prompt;

								if(prompt == "yes")
								{

								LifeWrite(L, position);
								cout<<"\n\nYOUR INFORMATION HAS BEEN SAVED!!\n";
								system("pause");
								
								system("cls");
								mainscreen();

								}else{
									system("pause");
								
								system("cls");
								mainscreen();
								}
 





i'm tryin to build a if statement to ask the user if they want to store the info
when there finish if not go back to main screen


but whenever i run the programme it doesnt read the "yes"

plz advice

Is This A Good Question/Topic? 1
  • +

Replies To: if statement problem

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: if statement problem

Posted 25 March 2009 - 09:02 AM

View PostMagnet, on 25 Mar, 2009 - 09:57 AM, said:

but whenever i run the programme it doesnt read the "yes"


prompt is of what type?
Was This Post Helpful? 0
  • +
  • -

#3 Magnet  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 49
  • Joined: 13-October 08

Re: if statement problem

Posted 25 March 2009 - 09:07 AM

View Postbrds, on 25 Mar, 2009 - 08:02 AM, said:

View PostMagnet, on 25 Mar, 2009 - 09:57 AM, said:

but whenever i run the programme it doesnt read the "yes"


prompt is of what type?



char prompt[4];

Was This Post Helpful? 0
  • +
  • -

#4 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: if statement problem

Posted 25 March 2009 - 09:11 AM

View PostMagnet, on 25 Mar, 2009 - 08:07 AM, said:

View Postbrds, on 25 Mar, 2009 - 08:02 AM, said:

View PostMagnet, on 25 Mar, 2009 - 09:57 AM, said:

but whenever i run the programme it doesnt read the "yes"


prompt is of what type?



char prompt[4];



i think you mean 'Yes'. Because it is a char, it will not compare properly with anything in double quotes.
Was This Post Helpful? 0
  • +
  • -

#5 Magnet  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 49
  • Joined: 13-October 08

Re: if statement problem

Posted 25 March 2009 - 09:19 AM

but i get these error

d:\virus++\insurance project\insurance project\drivermodule.cpp(187) : error C2446: '==' : no conversion from 'int' to 'char *'


d:\virus++\insurance project\insurance project\drivermodule.cpp(187) : error C2040: '==' : 'char [4]' differs in levels of indirection from 'int'
Was This Post Helpful? 0
  • +
  • -

#6 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: if statement problem

Posted 25 March 2009 - 09:25 AM

Switch its type to string.

or

#include <cstring>

/* And use */
if(strcmp(prompt, "Yes") == 1)


Was This Post Helpful? 0
  • +
  • -

#7 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: if statement problem

Posted 25 March 2009 - 09:26 AM

View PostMagnet, on 25 Mar, 2009 - 08:19 AM, said:

but i get these error

d:\virus++\insurance project\insurance project\drivermodule.cpp(187) : error C2446: '==' : no conversion from 'int' to 'char *'


d:\virus++\insurance project\insurance project\drivermodule.cpp(187) : error C2040: '==' : 'char [4]' differs in levels of indirection from 'int'



You will just like you will get a multi-char const if you compare it with == and 'yes.' Usually, char is used to compare one letter, anything else is better served as string.
Was This Post Helpful? 0
  • +
  • -

#8 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: if statement problem

Posted 25 March 2009 - 09:35 AM

View Postccubed, on 25 Mar, 2009 - 10:26 AM, said:

You will just like you will get a multi-char const if you compare it with == and 'yes.'


In what C++ compiler is
char prompt[4];

if(prompt == 'yes')
     cout<<"WTF?"<<endl;



even legal?
Was This Post Helpful? 0
  • +
  • -

#9 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: if statement problem

Posted 25 March 2009 - 10:22 AM

brds is correct
"Switch its type to string."

#include <string>  //up top


string prompt;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1