You are viewing our Forum Archives. To view or take place in current topics click here.
Help With a Program [TurboC++]
Posted:

Help With a Program [TurboC++]Posted:

Taylor
  • Summer 2020
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
I need help with a smaller program. Its a 10 question addition quiz, that randomizes the questions and numbers upon running the program each time.
The program itself works and runs, but there is a problem;

For example;
If the program asks the question, 9+2. I put in 11 (which is obviously correct), it will then tell me (or the user) Sorry, the right answer was 11. I have no clue how to fix this, and would like it if someone could take a look. Again, still very new to programming, but I am getting the hang of it. Just have no clue what is wrong with this.

My code:

#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<iomanip.h>

void Add();

int total,chc,Qs;
char chc2;

main()
{
randomize();
total=0;
Qs=0;
 do{
do{
 textbackground(BLACK);
 textcolor(LIGHTCYAN);
 clrscr();
 gotoxy(1,1);
 cprintf("You have %d/%d.",total, Qs);
 gotoxy(35,1);
 cprintf("Addition = 1");
 gotoxy(35,5);
 cprintf("End = 5 ");
 cin>>chc;

 if(chc==1)
 {
Add();
 }
if(chc==5)
    {
clrscr();
gotoxy(25,12);
cprintf("Are you sure want to quit? Y or N ");
cin>>chc2;
     }

 }while(chc!=5);
    }while(chc2=='n'||chc2=='N');

return 0;
}

void Add()
{
textbackground(GREEN);
textcolor(BLACK);
clrscr();
int g, num1, num2, ans, useans, right;

right=0;
for(g=1;g<=10;g=g+1)
   {

clrscr();

gotoxy(35,1);
cprintf("Question %d",g);
num1=random(51);
num2=random(51);
ans=num1+num2;
gotoxy(39,11);
cprintf("%d",num1);
gotoxy(38,12);
cprintf("+%d",num2);
gotoxy(39,13);
cin>>useans;

if(useans==ans)
   {
right=right+1;
clrscr();
  gotoxy(35,12);
cprintf("Good job!");
 delay(1000);
   }

if(useans!=ans)
   {
clrscr();
     gotoxy(35,12);
cprintf("Sorry, that's wrong! The answer was %d.",ans);
     delay(500);

   }

   }

textbackground(BLACK);
textcolor(CYAN);
clrscr();

  gotoxy(25,1);
  cprintf("You got %d out of 10 correct!",right);
getch();
total=total+right;
Qs=Qs+10;
}


Thanks to whoever can look at it and figure out what is wrong with it.
#2. Posted:
Taylor
  • Summer 2019
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Nevermind, I had a classmate look at it and she found the issue.

I was just missing the if statement that says Good Job for getting the answer correct.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.