Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Program gone crazy....
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Mon Oct 18, 2004 2:43 pm   Post subject: Program gone crazy....

well sort of. the program should do addition subtraction and multiplication to 2 random numbers, and it depends on the users choice. The user gets 5 questions per choice and then if he answers them right from the first time he gets 10 points if not he has 2 more tries which he gets 5 points for the first and 2 points for the third. if the question was answered wrong he gets no points and the program moves on to the next question ouf of 5.

for starterts i got and endless loop dont know why, if i type the wrong answer in then it gives the error msg and gives u 3 tries but it gives u endless amount of questions and i only need 5. If i get the answer right the program still gives u 3 tries eventhough u got it right.... i cant exit the program when i press 4 theres an endless loop... how to fix this stuff?


Now heres my code: Note( only try it with addtion cause i didnt do it for the other 2, gotta make addition work properly first)

code:


//Alex R
//A5Q1
//October 5th

#include <iostream.h >
#include <stdlib.h>
#include <time.h>

//menu
int menu()
{
int choice;
cout<<"Enter your choice\n";
cout<<"1. Addition\n2. Subtraction\n3. Multiplication\n4. Quit\n";
cin>>choice;
return (choice);
}





//random function
int ran ()
{
int x;
x=rand() % 100;
return (x);
}

//function for addition
int add (int a,int b)
{
return(a+b);
}


//function for subtraction
int subtract (int a,int b)
{
return(a-b);
}

//Function for multiplication
int multiply (int a,int b)
{
return (a*b);
}



//Answer calculation function
int answers(int a,int b,int choice)
{
//int answer;
int answer,userans,wrong,points,question,total;
wrong=0;
points=10;
total=0;
question=0;
int x,y;
while (wrong<3){
while (question<5){
start:
switch (choice){
case 1:cout<<a<<" + "<<b<<" = "<<endl;
answer=add(a,b);
cin>>userans;

if (answer!=userans && wrong==0){

cout<<"You have used 1 try out of 3, you can only get 5 points now\n";
points=5;
wrong++;
}
cout<<a<<" + "<<b<<" = "<<endl;
cin>>userans;

if (answer!=userans && wrong==1){
cout<<"You have used 2 try out of 3, you can only get 2 points now\n";
points=2;
wrong++;
}
cout<<a<<" + "<<b<<" = "<<endl;
cin>>userans;

if (answer!=userans && wrong==2){
cout<<"You have used 3 try out of 3, You get 0 points\nthe correct answer is "<<answer<<endl;
points=0;
wrong++;
question++;
a=ran();
b=ran();
wrong=0;
goto start;
}
if (answer=userans){
points=10;
question++;
a=ran();
b=ran();
break;
goto start;
}

case 2:cout<<a<<" - "<<b<<" = "<<endl;
subtract(a,b) ;
break;

case 3:cout<<a<<" * "<<b<<" = "<<endl;
multiply (a,b);
break;

case 4: cout<<"Bye!!";
break;
default:cout<<"The options are only 1-4.";
break;
}
}
}
}


int main ()
{

srand(time(0));
int x,y;

x=ran();
y=ran();
answers(x,y,menu());

return 0;
}


Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Mon Oct 18, 2004 3:55 pm   Post subject: (No subject)

your code is a mess... first off, you have no indents, second, your spacing is really off... and your code is way too complicated... i wrote a basic form of what u want to do in 5 min.. it doesnt have that point stuff u wanted.. but im not gona do all ur hw for you... copy it if u want, but i suggest understanding it and writing ur own

code:

#include <iostream.h >
#include <stdlib.h>
#include <time.h>

int menu(){
        int choice;
        cout<<"Enter your choice\n";
        cout<<"1. Addition\n2. Subtraction\n3. Multiplication\n4. Quit\n";
        cin>>choice;
        return choice;
}

int operation (int a,int b, int choice){
        if (choice == 1)
                return a + b;
        else if (choice == 2)
                return a - b;
        else
                return a * b;
}

void answers (int x, int y, int choice)
{
        int num;
        if (choice == 4)
                return;

        while(1)
        {
                if (choice == 1){
                        cout << "what is "<<x<<"+"<<y<<"?\n";
                        cin >> num;
                }
                else if (choice == 2){
                        cout << "what is "<<x<<"-"<<y<<"?\n";
                        cin >> num;
                }
                else if (choice == 3){
                        cout << "what is "<<x<<"x"<<y<<"?\n";
                        cin >> num;
                }
                if (num == operation(x,y,choice)){
                        cout<<"right\n";
                        break;
                }
                else
                        cout<<"wrong\n";
        }
}


int main (){
        srand(time(0));
        int x,y;

        x = rand() % 100;
        y = rand() % 100;
        answers(x,y,menu());

        return 0;
}
Andy




PostPosted: Mon Oct 18, 2004 3:58 pm   Post subject: (No subject)

sry.. forgot to put the loop in there, i edited it in now
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: