Quantcast

Maximum PC

It is currently Fri Nov 28, 2014 4:34 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Please help C++ n00b!
PostPosted: Tue Oct 19, 2004 2:42 pm 
Little Foot
Little Foot

Joined: Sun Aug 08, 2004 5:30 am
Posts: 147
Location: Qo'nos
Hi guys,

C++ n00b here, need some help with a homework assignment.

I'm trying to write a win32 console app in C++ using Visual Studio 2003, and am having issues with it.

The program I'm trying to write is basically a set of nested switch statements, and I can't seem to get the system to actually run the nested switches.

I wrote a simplified version of the app while trying to troubleshoot it. Here's what I've got:

Code:
#include<iostream>

using namespace std;

int main()
{
   
   //declares variables
   char var1;
   char var2;

   //user enters data
   cout<<"enter either a (1) or a (2): "<<endl;
   cin>>var1;
   cout<<"enter either another (1) or another (2): "<<endl;
   cin>>var2;

   //begins nested switch
   switch(var1)
   {
   case 1: //user enters a 1 as first number
      switch(var2)
      {
      case 1: //user enters 1 as second number
         cout<<"you entered a (1) and a (1)"<<endl;
         system("PAUSE");
         break;
      case 2: //user enters 2 as second number
         cout<<"you entered a (1) and a (2)"<<endl;
         system("PAUSE");
         break;
      }//end switch var2
      break;
   case 2: //user enters a 2 as first number
      switch(var2)
      {
      case 1: //user enters 1 as second number
         cout<<"you entered a (2) and a (1)"<<endl;
         system("PAUSE");
         break;
      case 2: //user enters 2 as second number
         cout<<"you entered a (2) and a (2)"<<endl;
         system("PAUSE");
         break;
      }//end switch var2
      break;
   }//end switch var1
   system("PAUSE");
   return 0;


}


Then the program essentially ends.

The final app shows this (example):


******************************************************

enter either a (1) or a (2):
1
enter either another (1) or another (2):
2
Press any key to continue...


******************************************************

Then the program ends. It never displays any of the cout statements written in the switches, and I can't figure out why.

Anyone get what I'm trying to do? What am I doing wrong?

*edit* Forgot about the green code formatting thing

Thanks in advance,
Treklink256


Top
  Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 6:53 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24236
Location: Granite Heaven
Why are they nested? This makes your program unnecessarily complicated.

I think (I'm not sure, because I can't find my reference and I don't use switches very often) that you need to put the character you are checking for in quotes ... so either of these should work:

Code:
case "1":

or
Code:
case '1':


If you were checking for an integer value, 1 would work .. but since you are checking for a character, I am pretty sure it needs to be in quotes.

At any rate, your program is probably failing because of some minor syntactical error like this one. :D


Top
  Profile  
 
 Post subject: Let's try something else...
PostPosted: Tue Oct 19, 2004 6:56 pm 
Little Foot
Little Foot

Joined: Sun Aug 08, 2004 5:30 am
Posts: 147
Location: Qo'nos
Well, I've been fiddling with this thing for a little while now, and tried to re-write it using nested "if" statements instead.

Here's what I wrote:

Code:
#include<iostream>

using namespace std;

int main()
{
   
   //declares variables
   char var1;
   char var2;

   //user enters data
   cout<<"enter either a (1) or a (2): "<<endl;
   cin>>var1;
   cout<<"enter either another (1) or another (2): "<<endl;
   cin>>var2;

   //begins nested ifs
   if(var1 =1)
   {
      if(var2 =1)
      {
         cout<<"you entered a (1) and a (1)"<<endl;
         system("PAUSE");
      }
      else if(var2 =2)
      {
         cout<<"you entered a (1) and a (2)"<<endl;
         system("PAUSE");
      }
   }
   else//var1 = 2
   {
      if(var2 =1)
      {
         cout<<"you entered a (2) and a (1)"<<endl;
         system("PAUSE");
      }
      else//var2 =2
      {
         cout<<"you entered a (2) and a (2)"<<endl;
         system("PAUSE");
      }
   }
   return 0;


}


When written like this, I always end up with the exact same output, regardless of what is entered.

Example:

******************************************************

enter either a (1) or a (2):
1
enter either another (1) or another (2):
2
you entered a (1) and a (1)
press any key to continue...


******************************************************

"You entered a (1) and a (1)". No, I didn't. At least I get SOME output this time...

Any thoughts anyone? Please?


Top
  Profile  
 
 Post subject: Does anyone actually read these "subject" things?
PostPosted: Tue Oct 19, 2004 7:17 pm 
Little Foot
Little Foot

Joined: Sun Aug 08, 2004 5:30 am
Posts: 147
Location: Qo'nos
Oh, hello!

Thanks for the suggestion, let me try it out a minute...

*type*type*type*

*build*

*runs exe*

Ha! It was the second one, the

Code:
case '1':


thing.

"unnecessarily complicated"... yhea, pretty much. Again this is a simplification of the actual program - not that the real thing is that much more complex - It's part of the assignment, I'm just trying to get it done with.

Thank you! I knew it was something stupid like that :oops:


Top
  Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 7:40 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
The reason your nested ifs were returning true was because you used = (the assignment operator) instead of == (the equivalency operator).


Top
  Profile  
 
 Post subject: Re: Does anyone actually read these "subject" thin
PostPosted: Tue Oct 19, 2004 7:51 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24236
Location: Granite Heaven
Treklink256 wrote:
Thank you! I knew it was something stupid like that :oops:


Absolutely, 100%, NOT stupid.

Computers are fanatical about syntax .. you get it right, or they do very very mischievous things to your program, your data and possibly your gf.

Practice, practice, practice.

;)


Top
  Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 8:02 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Quote:
they do very very mischievous things to your program, your data and possibly your gf.

Image


Top
  Profile  
 
 Post subject:
PostPosted: Sun Oct 24, 2004 8:40 pm 
8086
8086

Joined: Mon Aug 02, 2004 10:59 am
Posts: 50
That's an awsome image. Permission to copy it? [Y/N]


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 25, 2004 4:54 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
I didn't make it :)
Yes, it is an awesome image.

It's from www.roflcopter.com


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.