NEED CODE!!!

For my beginners C++ class...basic code but im really stuck on it. THANK YOU in advance. the instructions are as follows....

Write a program using a For statement in a pretest loop that allows a user to correctly output 5 correct arithmetic problems. input errors are not to count towards the number of correct arithmetic problems. Provide logical prompts to the user describing how the program is to work. Ask the userfor two numbers and a character representing the desired arithmetic operation. use a switch statement to determine which operation to perform. If the user enters faulty data, the program should output an appropriate error message. Display the answers to each problem as well as the error message associated with the third input.

input values 8 + 28
12 *12
24 x 15
144/12
96 - 18
1000 * 120
closed account (3qX21hU5)
So whats your problem... where are you stuck at... what do you have so far? These are all the questions I have for you because to be blunt we aren't going to do your homework for you. So post what you have so far and we can help you figure it out and remember to use code tags (Hint: the <> off to the right when replying).
to be honest all of it
just even a start to it would be nice
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

    using namespace std;

int main()
{

    return 0;
} 
anybody that can add onto that?
I was hoping you might.
cant. just really confused. finals and everything
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

bool successful();

int main()
{
    int success = 0;
    for (;success < 5;)
    {
      if (successful()) success ++;
    }
    return 0;
}

bool successful()
{
  return true;
}
sooo confused...i dont think weve used bool yet
How do you get the code to not count the incorrect input
closed account (3qX21hU5)
Why don't you post what you have so far and we can help you. The hardest part of this exercise will be the error handling. There are multiple ways of handling them, for example lets say the user inputs a incorrect operator. You could use something like this to handle the error for the operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int n1, n2;
    cin >> n1 >> n2;

    char op;
    cin >> op;

    switch (op)
    {
        case '+':
            cout << n1 << " + " << n2 << " = " << n1 + n2 << endl;
            break;
            
        // Handles all cases not specified
        default:
            cout << "You have entered a wrong operator please try again" << endl;
            break;
    }
}


Now this is only one way of doing it, but it might give you some ideas.
http://www.youtube.com/watch?v=4agL-MQq05E

this here is bucky, he is the most efficient way to learn c++, he is your teacher now this episode is the first i found its on recursion witch is most definatley too advanced for you today but watch enough of these and practice and you will get the basics in only a few days
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;

int main()
{
    //declare variables
    int num1 = 0;
    int num2 = 0;
    char operation = ' ';
    int counter = 1;
    int answer = 0;

    //get input
    cout << "Enter first number, then an arithmetic operation, and the second number." << endl;
        cout << "Multiplication: *" << endl <<
        "Division: /" << endl <<
        "Addition: +" << endl <<
        "Subtration: -" << endl << endl;

    //calculate operations

for ( counter = 1; counter < 6;)
{
    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter arithmetic operation: ";
    cin >> operation;
    cout << "Enter second number: ";
    cin >> num2;

    switch(operation)
    {
        case '*':
            answer = num1 * num2;
            break;
        case '/':
            answer = num1 / num2;
            break;
        case '+':
            answer = num1 + num2;
            break;
        case '-':
            answer = num1 - num2;
            break;
        default:
            cout << "Invalid input. Try Again." << endl << endl;
            operation = ':';
    }    //end switch

    if (operation != ':')
    {
        //display answer
        cout << endl << "Answer: " << answer << endl << endl;
        counter += 1;
    }
    else
        operation = ' ';
    //end if

}    //end for

    system("pause");
    return 0;
}    //end of main function 


does this look good? worked for me
i think its almost beautiful, if you want to please everyone get rid of system pause its a security problem in future codes and namespace std could me a bit more local (eg:std::cout) and a few comments and more self explanatory variable names and we have what people call beautiful code.

i am in the process of learning this now i have the basics down
Last edited on
closed account (3qX21hU5)
Don't know what your talking about a few more comments and more self explanatory variable names... If anything it could use less comments and the variables explain themselves ;)

Also if your going for perfection you don't need to declare int counter = 1; outside of your for loop. You can do this for (int counter = 1; counter < 6; counter++). Notice also that you can increment the counter inside it also.
Last edited on
@Zereo i mean self explanatory code and comments are one of the signs of a beautiful code.

I was told the more comments the better too (though my code is mute)
Last edited on
Topic archived. No new replies allowed.