Switch Function

Look at my code!

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1010 Views - Last Post: 05 December 2009 - 10:38 PM Rate Topic: -----

#1 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Switch Function

Posted 05 December 2009 - 07:03 PM

Im making a program that will help elemntary students learn multiplication. But I am having trouble with my function. I used switch to say different compliments to the student when they get a correct answer like: good job, excellent, etc but what is baffling is that all of the compliments show up at the same time when the student gets the answer correct. I just want the program to pick one response randomly. Take a look please :)

Also when the the student gets the wrong answer it will keep repeating the question. How do I limit that? (I want them to be able to try five times or something).

This post has been edited by sf18: 05 December 2009 - 08:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Switch Function

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Switch Function

Posted 05 December 2009 - 07:17 PM

You must add the word "break;" after each print line in each case. Otherwise it is going to fall through to the next case.

	case 0:
		printf("Very Good!\n");
		break;
	case 1:
		printf("Excellent!\n");
		break;
	case 2:
		printf("Nice Work!\n");
		break;
	case 3:
		printf("Keep Up the Good Work!\n");
		break;



This is by design. There are times where you want two individual cases to lead to the same set of lines of code. You may want it to fall through case 0 to case 1 and break when you have case 2 etc.

:)
Was This Post Helpful? 1
  • +
  • -

#3 ccubed  Icon User is offline

  • It's That Guy
  • member icon

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

Re: Switch Function

Posted 05 December 2009 - 07:17 PM

And Martyr2 Beats me to it. :^:

This post has been edited by ccubed: 05 December 2009 - 07:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Switch Function

Posted 05 December 2009 - 07:31 PM

Oh thank you so very much!
Was This Post Helpful? 0
  • +
  • -

#5 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Switch Function

Posted 05 December 2009 - 07:39 PM

I do have one more question though: when the the student gets the wrong answer it will keep repeating the question (which I do want). However, I do I limit that? (I want them to be able to try five times or something).
Was This Post Helpful? 0
  • +
  • -

#6 ccubed  Icon User is offline

  • It's That Guy
  • member icon

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

Re: Switch Function

Posted 05 December 2009 - 07:49 PM

continue to increase correct and add a case 5?
Was This Post Helpful? 0
  • +
  • -

#7 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Switch Function

Posted 05 December 2009 - 08:06 PM

Okay I really figured out the problem now let me see if I can explain this correctly: When the student gets the question wrong like what is 5 X 6 it will prompt 'try again', 'don't give', blah blah blah but when the student finally gets the correct answer it will keep repeating the same question
5 X 6 over and over endlessly. It wont go on to a different question. If you try the program you'll see.

This post has been edited by sf18: 05 December 2009 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 ccubed  Icon User is offline

  • It's That Guy
  • member icon

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

Re: Switch Function

Posted 05 December 2009 - 08:13 PM

With your current code, correct will never be anything but 0.
Was This Post Helpful? 0
  • +
  • -

#9 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Switch Function

Posted 05 December 2009 - 08:16 PM

I dont quite follow?? What should I do?
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Switch Function

Posted 05 December 2009 - 08:19 PM

First thing I notice: srand should only be called ONCE, at the very start of the program.

Second, you don't actually return anything from your functions.

In general, this is sloppy, thrown-together code with no real thought put into it. What is the point of the corr variable, to which you're assigning nothing?
Was This Post Helpful? 0
  • +
  • -

#11 ccubed  Icon User is offline

  • It's That Guy
  • member icon

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

Re: Switch Function

Posted 05 December 2009 - 08:24 PM

After looking..Oh wow.

A) You should only use correct once. As it stands, you have it both in main and the functions. Which is okay, but I Wouldn't recommend it.

B ) Your two loops need different controls. Right now, if they don't get the right answer the first time it will continue to loop through the same question because correct is set to 0 when they get it wrong and then never changed again. Now, you can get by without two controls, but when they get it right you need to set correct to 1 again.

This post has been edited by ccubed: 05 December 2009 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#12 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Switch Function

Posted 05 December 2009 - 08:37 PM

Thanks JackOfAllTrades you really helped! :(
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Switch Function

Posted 05 December 2009 - 08:46 PM

I told you you three things that were wrong with your program. I am not going to just write it; make an actual effort to take the things I just told you and apply them to your code. That's called LEARNING. Something you're supposed to be DOING, not looking for someone to just correct your code so you can hand it in without understanding it, only to get an A on the assignment, but fail your tests/final exam miserably.
Was This Post Helpful? 0
  • +
  • -

#14 srlester  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 53
  • Joined: 28-January 09

Re: Switch Function

Posted 05 December 2009 - 09:14 PM

View PostJackOfAllTrades, on 5 Dec, 2009 - 07:46 PM, said:

I told you you three things that were wrong with your program. I an not going to just write it; make an actual effort to take the things I just told you and apply them to your code. That's called LEARNING. Something you're supposed to be DOING, not looking for someone to just correct your code so you can hand it in without understanding it, only to get an A on the assignment, but fail your tests/final exam miserably.


Jack I think that was a little rough. I mean talking down to the guy isnt helping anything.


But anyway back to the problem.
It doesnt seem like much of a problem. You could either do it randomly with

srand(static_cast<unsigned int>(time(0))


or just set it if the answer is correct to use a variable the increased by one "++" until if 5 was the last one if (x==5) subtract 4 back from x to set it back to 1 and start over. I may write some code for that. I know there is probably and easier way but I'm pretty new to c++ but at least I'm trying to help. Just make sure if you do random to set it from 1 to 5.

Oh yeah make sure you #include <ctime>

This post has been edited by srlester: 05 December 2009 - 09:16 PM

Was This Post Helpful? 0
  • +
  • -

#15 sf18  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 148
  • Joined: 29-November 09

Re: Switch Function

Posted 05 December 2009 - 09:58 PM

Actually I am girl lol but thanks for the help and the comment :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2