Simple C++ program - help needed

This is a discussion on Simple C++ program - help needed within the C++ Programming forums, part of the General Programming Boards category; Hey guys, this is going to seem like a jumbled mess as I'm very tired and have been trying to ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Simple C++ program - help needed

    Hey guys, this is going to seem like a jumbled mess as I'm very tired and have been trying to figure this out for awhile. So, if you get irritated easily or don't have a minute or two to spare just ignore it, heh.

    I read the topics about posting, posting code, asking for help, etc. I have a legitimate need of programming help. It is for a class project in introduction to c++. We are allowed to see tutors. I have spent several hours on this program, seen a tutor at the school, and think I am at least somewhat close, but I am having some trouble puting it all together and understanding it.

    I am only a math minor (music major) and this is the only programming course I'll be taking. However, it is important I actually understand how it works and I need to write programs on in class tests, among other things. So, if you actually write me some code, it would benefit me greatly if you explained it or answered questions I might have about it.

    Ok, sorry for all the wording but I'm just trying to justify asking for help.

    Here is the program definition:

    Write a menu-driven program that will help an elementary school student learn multiplication. Use function rand to produce two positive one-digit integers. The program will ask a question such as: How much is 6 times 7?

    The student then types the answer. Your program checks the student's answer and prints the various comments:


    Responses to a correct answer:

    Excellent!
    Very Good!
    Nice work!
    Keep up the good work!

    Responses to an incorrect answer:

    No. Please try again.
    Wrong. Try once more.
    Don't give up!
    No. Keep trying.

    Use the random number generator to choose a number between 1 to 4 to select an appropriate response to each answer. Use a case select structure to issue the responses.

    The program should count the number of correct and incorrect responses typed by the student. After the student answers 10 questions, your program should calculate the percentage of correct responses.

    Then, modify the program to include two things:

    Allow the user to pick the type of arithmetic problems he or she wishes to study. An option of A means addition, S for subtraction, M for multiplication, D for division.

    Allow the user to enter a grade level capability. A grade level of 1 means only single digit numbers in the program, a grade level of 2 means to use the numbers as large as two digits, etc.



    here is what I've come up with so far.

    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    
    using namespace std;
    int main ()
    {
    
    int right=0,wrong=0;
    int leftoperand, rightoperand, response, answer, studentanswer, grade
    char category, opt
    
    cout<<"Please choose an arithmetic problem category:"<<endl;
    cout<<"A for addition, S for subtraction, M for multiplication, or D for division"<<endl;
    cin>>category
    cout<<"Now choose your grade level: 1 for single-digit numbers and 2 for two-digit numbers"<<endl;
    cin>>grade
    for(int i=0; i<10; i++)
    {
     SORRY ABOUT THIS PART- my tutor told me to use this formula, I don't entirely understand it
     but I know it will help differentiate between grade level 1 and grade level 2...
    
    leftoperand = (pow(10, grade - 1)) + rand()&#37;((pow(10,grade))-1);
    	rightoperand = (pow(10, grade - 1)) + rand()%((pow(10,grade))-1);
    
    
    
    rightoperand = (rand() %10) 
    
    answer=leftoperand*rightoperand // declaring the correct answer
    
    cout<<"How much is "<<leftoperand*rightoperand<<"?"<<endl; // asking student arithmetic questions
    cin>>studentanswer
    
    This next part is the only part I was happy with myself for, I think it is kind of close to being correct.
    Basically, it is the last half of the program without the two extra parts:
    
    for (int question=1; question<10; question++)
    {
    if (studentanswer==answer)
    {
    response = rand() %3;
    switch (response)
    {
    	right++ // increasing number of correct answers
    case 0:
    	cout<< "Excellent!"<<endl;
    	break;
    case 1:
    	cout<< "Very Good!"<endl;
    	break;
    case 2:
    	cout<< "Nice Work!"<<endl;
    	break;
    default:
    	cout<< "Keep up the good work!"<<endl;
    	break;
    }			// switch
    }			// else
    }			// for loop
    
    else (studentanswer!=answer)
    {
    response = rand() %3;
    switch (response)
    {
    	wrong++ // increasing number of incorrect answers
    case 0:
    	cout<< "No. Please try again."<<endl;
    case 1:
    	cout<< "Wrong. Try once more."<<endl;
    case 2:
    	cout<< "Don't give up!"<<endl;
    default:
    	cout<< "No. Keep trying."<<endl;
    }			// switch
    }			// else
    }			// for loop
    
    cout<<"You answered "<<right<<"questions correctly, and "<<wrong<<"questions incorrectly."<<endl;
    cout<<"This gives you a score of "<<(right/10)*100<<"out of 100"<<endl;
    
    
    return 0;
    
    }

    As you can see, the first half of the program is a mess. I am having trouble doing the parts where the user chooses the type of arithmetic problem and the grade level (1 digit or 2). I believe my program only works with multiplication at the moment. then, I am having trouble getting my program to ask the user a random question. I thought of typing up all the multiplication tables, etc, and using an if/else structure, but that has got to be the worst idea possible. I think maybe my problem is not entirely understanding the random number generator.

    I have been reading my c++ book (absolute c++) most of the day, and working on this and trying to piece it together for many hours, but apparently I just suck at programming. sorry if what I'm asking is unclear, I'm very tired at the moment. I will post more information when I wake up.

    If someone reads this and might be able to help, thanks. Otherwise, I'll add more information in the morning including what I was trying to do at various steps in the program, especially the beginning. I am willing to devote my whole weekend to getting this thing done, and am very eager to read peoples ideas and opinions.
    Last edited by CornedBee; 10-06-2007 at 03:37 AM. Reason: Broke lines in code section to preserve site layout.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I've broken the over-long lines in the code section so that they don't mess up the site layout. However, the code is still poorly formatted. Make sure you've got proper indentation, because as it is it's very hard to read. And put some spaces between operators and arguments!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    Well you need
    a) a fair number of ; just to make it compile
    b) a decent approach to indentation to make people want to look at it. Unindented code just turns people away from your post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Code:
    int leftoperand, rightoperand, response, answer, studentanswer, grade
    missing ;
    Code:
    int leftoperand, rightoperand, response, answer, studentanswer, grade;
    Code:
    char category, opt
    missing ;
    Code:
    char category, opt;
    Code:
    cin>>category
    missing ;
    Code:
    cin>>category;
    Code:
    cin>>grade
    missing ;
    Code:
    cin>>grade;
    Code:
    rightoperand = (rand() %10) 
    
    answer=leftoperand*rightoperand 
    
    cin>>studentanswer
    missing ;

    Code:
    rightoperand = (rand() %10); 
    
    answer=leftoperand*rightoperand;
    
    cin>>studentanswer;
    Code:
    right++ 
    wrong++
    missing ;

    Code:
    right++;
    wrong++;
    And another thing.....
    I suggest you do some Data Validation in this part:
    Code:
    cout<<"Please choose an arithmetic problem category:"<<endl;
    cout<<"A for addition, S for subtraction, M for multiplication, or D for division"<<endl;
    cin>>category
    for example:

    Code:
    if(category == 'A' || category == 'a')
    the problem is you dont use the variable: category so the user can put the letter P and the program will run with no errors.

    Hope this helps.

    P.D. I could be wrong since this is my first attempt of helping someone in this forum...

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Thanks for the help, especially Conspiracy! I did the data control you mentioned and I think it was a good idea. I've put the segments of my program together and corrected as many errors as I could figure out.

    Sorry for the poor formatting. This is the 2nd program I've ever written on my own and I'm not sure how to make it look appealing

    I decided to use 4 sections of very similar code, each with a different operator possibility, +,-,/,*.

    I think this will work if I can get the program to run, but I still can't get it to run. But, now there are only a few errors.

    The main problem I am having is getting the pow function to work correctly. I read that it does not function with integers but works with doubles, but my formula only works with integers because of the &#37; I used in the pow line of code not working with doubles.

    Here is the exact error I'm getting:

    Error 1 error C2668: 'pow' : ambiguous call to overloaded function, line 53

    It gets this error 4 times on lines 53 and 55, both of my pow code lines. I think I need the pow line of code to have that formula because otherwise, the grade entered by the user won't function correctly and my operands won't be the right number of digits.


    The next error I'm getting is

    Error 5 error C2679: binary '<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
    line 72

    I think this error is related to the pow error, and I get it 4 times.

    The only other error I get, I am also getting 4 times.

    Error 6 error C2143: syntax error : missing ';' before '{'
    This occurs at lines 84, 129, 175, and 220.

    In other words, this error occurs in each of my 4 arithmetic sections (addition, subtraction, multiplication, division). It occurs in the else section (student entered incorrect answer).


    Here is my program, sorry for posting it again but it is quite different now, I think in a good way:

    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    
    using namespace std;
    
    /* 
    
    
    This is a program that assists elementary school students in learning 
    addition, subtraction, multiplication, and division. 
    The user chooses his or her grade level and the type of problems to be studied, 
    and the program begins.
    
    Variable Dictionary:
     Integers:
    	right : The number of questions answered correctly by the user. 
    			Used to determine percentage of correct answers.
    	wrong : The number of questions answered incorrectly by the user.
    	leftoperand, rightoperand : The random numbers generated to form an arithmetic operation.
      	response : The random text response chosen for a students correct or incorrect answer.
    	answer : The correct answer to the randomly chosen arithmetic operation.
    	studentanswer : The answer the student inputs to the randomly chosen arithmetic operation.
    	grade : Input from the user that determines the number of 
    			digits that will be used in the arithmetic operations.
    
     Characters:
    	category : Determines which type of arithmetic operations will be used. Chosen by user.
    
    */
    
    int main ()
    {
    int right=0,wrong=0;	/* Declaring and setting number of questions answered either
                                         incorrectly or correctly to 0. */
    int leftoperand, rightoperand, response, answer, studentanswer, grade;
    char category;
    
    cout<<"Please choose an arithmetic problem category:"<<endl; 
    cout<<"A for addition, S for subtraction, M for multiplication, or D for division"<<endl;
    cin>>category; // getting operand category from user
    cout<<"Now choose your grade level: 1 for single-digit numbers, 2 for two-digit numbers, etc."<<endl;
    cin>>grade; // getting grade level from user
    
    for(int i=0; i<10; i++) // for loop used to limit number of questions to 10.
    {
    
    leftoperand = (pow(10, grade - 1)) + rand()%((pow(10,grade))-1); 
    // generate a random number  with as many digits as the user chose based on grade level 
    
    rightoperand = (pow(10, grade - 1)) + rand()%((pow(10,grade))-1); 
    // same, for right side of arithmetic operation
    
    if (category == 'A' || category == 'a') // addition problems category section
    {
    answer = leftoperand + rightoperand;
    cout<<"How much is "<<leftoperand<<" + "<<rightoperand<<"?"<<endl; // asking student arithmetic question
    cin>>studentanswer;
    	if (studentanswer==answer) // determining if student answered question correctly
    {
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	right++; // increasing number of correct answers
    case 0:
    	cout<< "Excellent!"<<endl;
    	break;
    case 1:
    	cout<< "Very Good!"<endl;
    	break;
    case 2:
    	cout<< "Nice Work!"<<endl;
    	break;
    default:
    	cout<< "Keep up the good work!"<<endl;
    	break;
    }			// switch
    }			// nested if
    
    else (studentanswer!=answer)
    {
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	wrong++; // increasing number of incorrect answers
    case 0:
    	cout<< "No. Please try again."<<endl;
    case 1:
    	cout<< "Wrong. Try once more."<<endl;
    case 2:
    	cout<< "Don't give up!"<<endl;
    default:
    	cout<< "No. Keep trying."<<endl;
    }			// switch
    }			// nested else
    }			// ending if category == addition section
    
    else if (category == 'S' || category == 's')
    
    {
    answer = leftoperand - rightoperand;
    cout<<"How much is "<<leftoperand<<" - "<<rightoperand<<"?"<<endl; // asking student arithmetic question
    cin>>studentanswer;
    	if (studentanswer==answer)
    {
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	right++; // increasing number of correct answers
    case 0:
    	cout<< "Excellent!"<<endl;
    	break;
    case 1:
    	cout<< "Very Good!"<endl;
    	break;
    case 2:
    	cout<< "Nice Work!"<<endl;
    	break;
    default:
    	cout<< "Keep up the good work!"<<endl;
    	break;
    }			// switch
    }			// nested if
    
    else (studentanswer!=answer)
    {
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	wrong++; // increasing number of incorrect answers
    case 0:
    	cout<< "No. Please try again."<<endl;
    case 1:
    	cout<< "Wrong. Try once more."<<endl;
    case 2:
    	cout<< "Don't give up!"<<endl;
    default:
    	cout<< "No. Keep trying."<<endl;
    }			// switch
    }			// nested else
    }			// ending if category == subtraction section
    
    
    else if (category == 'M' || category == 'm')
    
    {
    answer = leftoperand * rightoperand;
    cout<<"How much is "<<leftoperand<<" * "<<rightoperand<<"?"<<endl; // asking student arithmetic question
    cin>>studentanswer;
    	if (studentanswer==answer)
    	{
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	right++; // increasing number of correct answers
    case 0:
    	cout<< "Excellent!"<<endl;
    	break;
    case 1:
    	cout<< "Very Good!"<endl;
    	break;
    case 2:
    	cout<< "Nice Work!"<<endl;
    	break;
    default:
    	cout<< "Keep up the good work!"<<endl;
    	break;
    }			// switch
    }			// nested if
    
    else (studentanswer!=answer)
    {
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	wrong++; // increasing number of incorrect answers
    case 0:
    	cout<< "No. Please try again."<<endl;
    case 1:
    	cout<< "Wrong. Try once more."<<endl;
    case 2:
    	cout<< "Don't give up!"<<endl;
    default:
    	cout<< "No. Keep trying."<<endl;
    }			// switch
    }			// nested else
    }			// ending if category == multiplication section
    
    
    else if (category == 'D' || category == 'd')
    {
    answer = leftoperand / rightoperand;
    cout<<"How much is "<<leftoperand<<" / "<<rightoperand<<"?"<<endl; // asking student arithmetic question
    cin>>studentanswer;
    	if (studentanswer==answer)
    {
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	right++; // increasing number of correct answers
    case 0:
    	cout<< "Excellent!"<<endl;
    	break;
    case 1:
    	cout<< "Very Good!"<endl;
    	break;
    case 2:
    	cout<< "Nice Work!"<<endl;
    	break;
    default:
    	cout<< "Keep up the good work!"<<endl;
    	break;
    }			// switch
    }			// nested if
    
    else (studentanswer!=answer)
    {
    response = rand() %3; // random number generator for computer response to student's answer
    switch (response)
    {
    	wrong++; // increasing number of incorrect answers
    case 0:
    	cout<< "No. Please try again."<<endl;
    case 1:
    	cout<< "Wrong. Try once more."<<endl;
    case 2:
    	cout<< "Don't give up!"<<endl;
    default:
    	cout<< "No. Keep trying."<<endl;
    }			// switch
    }			// nested else
    }			// ending if category == division section
    
    else // If the user entered invalid character
    {
    i--; // decreasing the number of questions by 1 because a question is not actually asked.
    cout<< "Error!"<<endl;
    }
    } // for loop
    
    cout<<"You answered "<<right<<"questions correctly, and "<<wrong<<"questions incorrectly."<<endl;
    cout<<"This gives you a score of "<<(right/10)*100<<"out of 100"<<endl;
    
    
    return 0;
    
    }
    Last edited by Kinnison; 10-06-2007 at 01:38 PM.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Error 5 error C2679: binary '<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
    line 72
    I think this error is related to the pow error, and I get it 4 times.
    The error is not related to the pow function problem, its because you wrote <endl; and its <<endl;
    And you get it for times because you have this part:
    Code:
    switch (response)
    {
    	right++; // increasing number of correct answers
    case 0:
    	cout<< "Excellent!"<<endl;
    	break;
    case 1:
    	cout<< "Very Good!" << endl;
    	break;
    case 2:
    	cout<< "Nice Work!"<endl;
    	break;
    default:
    	cout<< "Keep up the good work!"<<endl;
    	break;
    4 times, so you have the same < instead of << extraction operand

    The only other error I get, I am also getting 4 times.

    Error 6 error C2143: syntax error : missing ';' before '{'
    This occurs at lines 84, 129, 175, and 220.
    You get this error because you wrote:
    Code:
    else(studentanswer != answer)
    its supposed to be like this:
    else
    if(studentanswer != answer)
    Again you have the same problem 4 times..... I think its because you copied and pasted the same code line...

    And finally the pow problem....

    The pow function is supposed to take two double arguments and display a double result:
    pow(x, y)
    you have this:
    Code:
    (pow(10, grade- 1))
    I think its better to put it like this:
    Code:
    (pow(10, grade)- 1)
    since the grade and the leftoperand variables are type int the pow function dosent work

    BUT if you put those variables as double then you cant use the % operator because it only works with type int arguments:
    example: 5 % 2 = 1.

    So you have a tricky problem and I cant seem to find the answer.

    Are you COMPLETELY sure you have to use % and not / ?

    I hope this helps

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    since the grade and the leftoperand variables are type int the pow function dosent work
    There is an overloaded version of std::pow() that takes a double and an int as arguments, so that function will be called. Still, even without that an implicit cast should be made, and more importantly, your version changes the calculation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'm not sure how to make it look appealing
    Well, for a start, make sure that everything between braces is one level of indentation to the right from its surrounding area:
    Code:
    void func()
    {
        // One level deeper.
        int foo = 5;
        if(foo == 5) {
            // Another level deeper.
            foo = 6;
        }
    }
    This is the most basic, most important rule. Other rules are subject to personal taste.

    What code editor do you use? Most should enforce this rule automatically anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    I use visual studio. Thanks for all the help guys. I fixed every problem now except for the darn pow function problem.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  4. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM
  5. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 06:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21