Archived

This topic is now archived and is closed to further replies.

SSJGaz

If Inside an If

Recommended Posts

Is it possable to have an IF inside another if? it doesnt seem to be working for me.
if( fullselection == 1)
{
cout <<"You Have Chosen To View Basic System Specs.\n" << endl ;
Sleep(1000);
cout <<"*Lynx K7SOM Mainboard" << endl;
Sleep(400);
cout <<"*Max CPU''s - One. One 500Mhz Manufactured By DMA Supplied." << endl;
Sleep(400);
cout <<"*Hard Disk Slots - Two. 24GB Supplied." << endl;
Sleep(400);
cout <<"*Modem - 1Mb DSL Supplied." << endl;
Sleep(400);
cout <<"*Cost - 16500 CR\n" << endl;
cout <<"Would You Like To Purchase This System? (Y or N)" << endl;
cin >> &buybasicsystem[0];
if( buybasicsystem == "y")
cout <<"Test" << endl;
[/soure]

when i choose to buy the system the word TEST doesnt show up, it just says Press any key to continue. 

Share this post


Link to post
Share on other sites
Yes.


if(fullselection == 1)
{
//...

if(buybasicsystem == "y")
{
cout << "Test" << endl;
}
else
{
cout << "Thank you for shopping with us." << endl;
}
}

You don''t need the else. It was just an example.

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

Share this post


Link to post
Share on other sites
There is nothing wrong with having an if inside of an if. Your problem is using pointers. &buybasicsystem[0] is essentially the same thing as buybasicsystem, without the [0]. The name of an array is simply a pointer to the first element, so when you are doing if( buybasicsystem == "y") it is testing the address, not the value.

Change it to:

cin >> buybasicsystem[0];
if( buybasicsystem[0] == "y")

Share this post


Link to post
Share on other sites
Oh yes... When you do "y" that treats it as a string, which also includes the null terminator. Try changing it to ''y'' to treat it as a char and see what happens.

Share this post


Link to post
Share on other sites
quote:
Original post by Raloth
Oh yes... When you do "y" that treats it as a string, which also includes the null terminator. Try changing it to ''y'' to treat it as a char and see what happens.


error C2440: ''initializing'' : cannot convert from ''char [1]'' to ''char''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

o0

Share this post


Link to post
Share on other sites
change those last couple of lines to this ....




cin >> &buybasicsystem[0];
if( buybasicsystem[0] == 'y')
cout <<"Test" << endl;



you were comparing a character to a string.


edit: typo

[edited by - Tiffany_Smith on July 27, 2003 1:40:41 PM]

Share this post


Link to post
Share on other sites
That just compares the first letter of the char array. If you want to compare as a string:

#include <string>
cin >> &buybasicsystem[0];
if( strcmp(buybasicsystem, "y") == 0 )
cout <<"Test" << endl;

Share this post


Link to post
Share on other sites
Just a suggestion, Sleep() is annoying. Don''t use it for fancy purpores.

Let me tell you a true story:
A kid was making a game. When you start the game, you have to look at the title screen for about 5 WHOLE SECONDS!!!

Now, you see, I asked him, "Why do that?". He replied, "Because I wanted people to see the screen." I countered, "What if the person already played the game a lot and don''t want to wait for 5 seconds?". "Well... to bad for him.", he states.

True story... Morale: Don''t use sleep unless you really need it. (Your teacher says: "Use sleep.", then you do. Actually, counter your teacher with this true story.)




Visit my game programming website at www.wiseelben.com

Share this post


Link to post
Share on other sites
quote:
Original post by Ironica
That just compares the first letter of the char array. If you want to compare as a string:

#include <string>
cin >> &buybasicsystem[0];
if( strcmp(buybasicsystem, "y") == 0 )
cout <<"Test" << endl;



Well, comparing with the first character is good too because it will also match with "y", "yes", "yeah", "yep", etc.

Share this post


Link to post
Share on other sites
quote:
Original post by smart_idiot
Well, comparing with the first character is good too because it will also match with "y", "yes", "yeah", "yep", etc.




Although, it may also be a bad thing, because comparing the first byte will also match "you stupid son of a #^&*@, why the #^(& would I want to do something so abysmally moronic?" which of course does not mean "yes."

Share this post


Link to post
Share on other sites
quote:
Original post by ApochPiQ
quote:
Original post by smart_idiot
Well, comparing with the first character is good too because it will also match with "y", "yes", "yeah", "yep", etc.




Although, it may also be a bad thing, because comparing the first byte will also match "you stupid son of a #^&*@, why the #^(& would I want to do something so abysmally moronic?" which of course does not mean "yes."

It''s all a matter of interpretation.



"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"

"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."

Share this post


Link to post
Share on other sites
quote:
Original post by SoulSkorpion
Um... why not just do

if(buybasicsystem[0] == ''y'' || buybasicsystem[0] == ''Y'')  
instead of messing around with strcmp?

Ironica, was giving an example of how to compare as a string, anyway ...
if(toupper(buybasicsystem[0]) == ''Y'') would be better than that.

I dont see why buybasicsystem is an array here. (but it is early, maybe im missing something) :\

Share this post


Link to post
Share on other sites