Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Debug Project 4 (Easy to Medium)

Think you can spy out that little nasty bug? Try debugging some code in here

Debug Project 4 (Easy to Medium)

Postby Marss++ on Mon Jul 21, 2008 11:08 pm

This program will run with no errors but will not do what it is suppose to do.

Objective is:


User will input a number between 1 and 5.

Whatever the number is the computer prints out "You have chosen number ... "

Here is the code:

Code: Select all

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
   int a;
   cout << "Please input a number 1-5" << endl;
   cin >> a;

   switch(a)
   {
   case 1: cout << "You chose 1" << endl;
   case 2: cout << "You chose 2" << endl;
   case 3: cout << "You chose 3" << endl;
   case 4: cout << "You chose 4" << endl;
   case 5: cout << "You chose 5" << endl;
   }
   return 0;
}




See if you can spot the error without running it first. Then if you cannot do that. Run it and it should be clear to you.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby antiRTFM on Mon Jul 21, 2008 11:52 pm

dude you have actually taught me something i didnt know here... but i wont give it away till i see some user answers :)

i think ill need to update a video based on this

thou shalt know this and taketh this to heart; never underestimate the complexity of a line -let alone a snippet- of c++ code. You think what you are seeing here is just some any-day c++ code involving a switch statement? think again!
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Postby asib on Wed Jul 23, 2008 12:31 am

You need to add to the switch :

Code: Select all
default : cout << "You chose " << a << endl;


So it can cater for values above 5.
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby oopalonga on Wed Jul 23, 2008 11:44 am

I think you need to add
Code: Select all
break;
after each case; otherwise the compiler will automatically say the user has chosen the other numbers as well.

Finished code:

Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
   int a;
   cout << "Please input a number 1-5" << endl;
   cin >> a;

   switch(a)
   {
   case 1: cout << "You chose 1" << endl;
      break;
   case 2: cout << "You chose 2" << endl;
      break;
   case 3: cout << "You chose 3" << endl;
      break;
   case 4: cout << "You chose 4" << endl;
      break;
   case 5: cout << "You chose 5" << endl;
      break;
   }

   char q;
   cin >> q;
   return 0;
}



Thanks for this example. I learned switchs and breaks :)
oopalonga
 
Posts: 17
Joined: Sun Jul 20, 2008 7:53 pm

Postby asib on Wed Jul 23, 2008 12:31 pm

Woops, I'm wrong xD
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby Marss++ on Wed Jul 23, 2008 12:42 pm

Both of you are correct.

Only the problem is that asib, It's not 100% necessary to have a default switch although it is recommended.


But yesss... Without the break the compiler will not stop after the first switch statement. It will just run through all of them after the one you entered.

Great Job.

The break statement is not only useful. It is absolutely necessary in this situation.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby antiRTFM on Wed Jul 23, 2008 9:19 pm

what i didn't know is that switch statements will execute ALL and EVERY case after the first matching case, regardless if the case really matches the tested switch object or not. I thought it will execute all cases that match the condition ONLY if they actually DO match the tested switch object (which may be more than one case).

Quote from the standard (section 6.4.2 - switch statements)

if one of the case constants is equal to the value of the condition, control is passed to the statement following the matched case label ... [the] labels in themselves do not alter the flow of control which continues unimpeded across such labels. To exit ... see break
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Postby Marss++ on Thu Jul 24, 2008 12:32 pm

I believe if you chose 3. It would say the 3 through 5.

if you did 2 it would say the 2 through 5.

if you just inputed 5 it would just say five.


That's what I think is going to happen here. Haven't actually tested it out but that's my guess.

Good job though!
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Re: Debug Project 4 (Easy to Medium)

Postby Argio on Mon Dec 15, 2008 6:01 am

Hai again!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Here is the code:

Code: Select all


#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
   int a;
   cout << "Please input a number 1-5" << endl;
   cin >> a;

   switch(a)
   {
   case 1: cout << "You chose 1" << endl;
   case 2: cout << "You chose 2" << endl;
   case 3: cout << "You chose 3" << endl;
   case 4: cout << "You chose 4" << endl;
   case 5: cout << "You chose 5" << endl;
   }
   return 0;
}


In case you didn't notice the error is on lines 14-18.
Code: Select all
case 1: cout << "You chose 1" << endl;
case 2: cout << "You chose 2" << endl;
case 3: cout << "You chose 3" << endl;
case 4: cout << "You chose 4" << endl;
case 5: cout << "You chose 5" << endl;


Oh noes! What do we do Argio?????????????????????????????

Code: Select all

case 1: cout << "You chose 1" << endl;
break;
case 2: cout << "You chose 2" << endl;
break;
case 3: cout << "You chose 3" << endl;
break;
case 4: cout << "You chose 4" << endl;
break;
case 5: cout << "You chose 5" << endl;
break;


Come on Argio, how does that work??? :!: :?: :!: :?:

I don't even need to make strange references to real life objects... Yay!

Well, thing of it this way...

Before I fixed the program, it did not know to stop running through the checks when it found the right one. This resulted in the following things:

If I chose 2 I would get 2-5
If I chose 5 i would get 5 :p

So a simple break statement is needed. When you place a break statements it tells the computer to skip the rest of the if or switch statements when it finds the correct one. :D

YAY!
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests