Project Help!

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Project Help!

Postby CNwB » Fri Sep 26, 2003 9:24 pm

Ok I finally got this project to work with all the help I was able to get, but for some reason I get get the numbers to randomize, this is the scenerio, I run the program and it ask this # times this #(which I want the program to do) then if the user gets it wrong it will ask the same question till the user gets it right( looking good so far ). Now when the user gets it correct it says "Good Job" BUT.. when it prompts for a another question it asks the same one which I don't want it to. Also with my "Switch statement" that would generate different responses for the output of each answer the user inputs, it seems that it would only generate case 0 and case 1's printf statement and not case 2, why is that???. Help please!!! Thanks in advance.

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int ans,x,y;

srand( time( NULL ));
x= rand() % 4;
y= rand() % 4;


clrscr();

printf("How much is %d times %d?", x, y );
scanf("%d", &ans);

while ( ans !=-1)
{
  if (ans==x*y){
   printf("Very Good!\n");
  printf("How much is %d times %d?", x, y);
  scanf("%d", &ans);

   switch (ans){

      case 0:
      printf("Excellent Work!\n");
      break;

      case 1:
      printf("Nice Work!\n");
      break;

      case 2:
      printf("Keep up the good work!\n");
      break;

      default:
      printf("No answer were inputted!!!");
      break;
      }
}
else if (ans!=x*y) {
   printf("Try again\n");
   printf("How much is %d times %d?", x, y);
   scanf("%d", &ans);
    }
   else{
      printf("How much is %d times %d?", x, y);
    scanf("%d", &ans);
   }
}
getch();

return 0;

}
CNwB
 

Postby Wizard » Sat Sep 27, 2003 7:53 am

You're only setting x and y once, right at the start.
Somewhere in the
if (ans==x*y){
block, set x and y to new random variables, as you did at the start. Don't call srand again, though.
Also, if I understand what you want, put the switch statements before asking the new question. What it currently does is, if you get it right, it then asks the same question again (which will be fixed with the new rand statements) and based on the answer to that SECOND question, it outputs the case. Also, you're only considering answers of 0, 1, and 2. If the numbers are, say, 2 and 3 which = 6, then the default will be shown.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Postby CNwB » Sat Sep 27, 2003 12:57 pm

Cool, I did what you have said Wizard, were I added the int of x & y within the if ( ans == x * y) statement and it went fine calling random numbers rather than calling the same random numbers again, thanks. As for the switch statement you were correct on that if the answer were 2, and 3 which = 6, that it would print out the default. Should I use the modulus operator in the switch so it would'nt print out the default statement?? ex. switch (ans % 2) something like that??
CNwB
 

Postby CNwB » Sat Sep 27, 2003 1:11 pm

Ok I've added a few modz here on the "if (ans!=x*y)" using the switch but for some reason that switch doesn't want to print. Now it only prints "No. Please try again" or No answer were inputted". HELP??? PLS.
CNwB
 

Postby CNwB » Sat Sep 27, 2003 1:13 pm

Sorry about the last post I meant to show the codes, here it is:

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int ans,x,y;

srand( time( NULL ));
x= rand() % 4;
y= rand() % 4;


clrscr();

printf("How much is %d times %d?", x, y );
scanf("%d", &ans);

while ( ans !=-1)
{
  if (ans==x*y){
   printf("Very Good!\n");
x= rand() % 4;
y= rand() % 4;


   switch (ans){

      case 0:
      printf("Excellent Work!\n");
      break;

      case 1:
      printf("Nice Work!\n");
      break;

      case 2:
      printf("Keep up the good work!\n");
      break;
      }

  printf("How much is %d times %d?", x, y);
  scanf("%d", &ans);
}
else if (ans!=x*y) {
   printf("No. Please try again.\n");
   

   switch (ans){

      case 4:
      printf("Wrong. Try once more.\n");
      break;

      case 5:
      printf("Don't give up!\n");
      break;

      
      case 6:
      printf("No. Keep trying\n");
      break;

      default:
      printf("No answer were inputted!!!");
      break;
      }
   printf("How much is %d times %d?", x, y);
   scanf("%d", &ans);
    }

}
getch();

return 0;

}
CNwB
 

Postby Wizard » Sat Sep 27, 2003 4:01 pm

Hi,
unless you use a case for every single possible answer the user could put (don't do that) you should use your switch (ans%3) solution. Then your cases will be 0, 1, and 2 only.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Postby CNwB » Sat Sep 27, 2003 5:22 pm

Can you check if this one looks good... close? or not? Improvements???

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int ans,x,y;

srand( time( NULL ));
x= 1 + rand() % 4;
y= 1 + rand() % 4;


clrscr();

printf("How much is %d times %d?", x, y );
scanf("%d", &ans);

while ( ans !=-1)
{
  if (ans==x*y){
   
x= 1 + rand() % 4;
y= 1 + rand() % 4;


   switch (ans){

      case 0:
      printf("Very Good!\n");
      break;
      
      case 1:
      printf("Excellent Work!\n");
      break;

      case 2:
      printf("Nice Work!\n");
      break;

      case 3:
      printf("Keep up the good work!\n");
      break;

      
      case 4:
      printf("Very Good!\n");
      break;
      
      case 6:
      printf("Excellent Work!\n");
      break;

      case 8:
      printf("Nice Work!\n");
      break;

      case 9:
      printf("Keep up the good work!\n");
      break;

      case 12:
      printf("Very Good!\n");
      break;
      
      case 16:
      printf("Keep up the good work!\n");
      break;
      }


  printf("How much is %d times %d?", x, y);
  scanf("%d", &ans);
}
else if (ans!=x*y) {
   printf("How much is %d times %d?", x, y);
   scanf("%d", &ans);

   switch (ans % 3){

      case 0:
      printf("No. Please try again.\n");
      break;

      case 1:
      printf("Wrong. Try once more.\n");
      break;

      case 2:
      printf("Don't give up!\n");
      break;

      case -1:
      printf("No answer were inputted!!!\n");
      break;
      }
   
    }

}
getch();

return 0;

}
CNwB
 

Postby CNwB » Sat Sep 27, 2003 5:33 pm

Also there seems to be a slight problem with the two switch statements, it seems fine when the user gets the question always right, then if the user gets the question wrong it shows the correct response (good so far), now when it ask the same question, the user gets it wrong (on purpose lets say) it then show the correct output, once it ask the same question again the user puts the correct answer and it output one of the wrong answer response and the correct answer response.
ex.
How much is 2 * 4?
3
No. Try again (looks good here)
How much is 2 * 4?
8
Don't give up! (What happen here???)
Nice work! (Correct response)
How is 3 * 2?
CNwB
 


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 3 guests

cron