I need assistance with loops

Hi,
I want to create a loop inside a calculator which would ask the user to input the operation and the numbers again and would keep on pestering the user till they get it right.

I used the word assistance in the title because the word help is taboo.



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Future Note for Myself: This version includes square roots and also exponents.
Also, this series no longer uses numbers for operations. Numbers for operation have been replaced
with a character constant. It still uses 'if statements'. I am still working on a way to create
a loop to make the user continually input data if it is incorrect. Next version: Work on including trignometric formulas in the calculator. */

#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main()
{
    double first;//teh first number
    char operation;//operation that is inputted and then later used in if statement
    double second; // tehe second number
    double result; // the first result which is displayed
    
cout << "Calc v3 "<< endl; 
           
           
           cout<< "If you want to find a square root or use exponents";
           cout<< " please enter random stuff until the first part of the program is executed"<<endl;
           
           cout<< ""<<endl;
           cout<<""<<endl;
           
           cout<< " Enter the expression you wish to calculate"<<endl;
           cout<< ""<<endl;
           cout<< " For example: '1*2'"<<endl;
           
           
           cin >> first;
           cin >> operation;
           cin >> second;
           
           if (operation == '+') result = first + second;
           if (operation == '-') result = first - second;
           if (operation == '*') result = first * second;
           if (operation == '/') result = first / second;
           
           
           
           cout<< " The answer is "<< result<<endl;
           
           
           cout << ""<<endl;
           cout << ""<<endl;
           cout << ""<<endl;
           
           cout<< "If you wish to find a square root or use exponents."<<endl;
           cout<< "If not, now is the time to press the 'X' at the top right corner of this window"<<endl;
           
           cout<<""<<endl;
           cout<<""<<endl;
           cout<<""<<endl;
           
           
           
           long double second_input;
           long int second_result;
           long double exponent;
           string second_choice;
           
           
           cout<< " Please enter the number"<<endl;
           cin >> second_input;
           
           cout<< " Enter exp to repeatedly multiply the number"<<endl;
           cout<< " Enter sqrt to find the square root of the number"<<endl;
           cin >> second_choice;
           
           cout<< " Enter how many times the number should be multiplied"<<endl;
           cin >> exponent;
        
           if (second_choice=="exp") result= pow(second_input,exponent);
           if (second_choice=="sqrt") result= sqrt(second_input);
           
           return 0;
   
}


ty in advance
Well i guess i didn't need to post the whole code. i didn't understand how the while and for loops worked. can someone help me? sorry i meant to type assist.
Last edited on
If you're trying to be a smartass with "help is taboo", it's really working, because I can really feel the smartass vibe coming out of it.
There's nothing taboo about "help". What's supposed to be taboo is homework, and whether you say help or assist, I (and probably, plenty of other people) can tell that this is homework. It's a standard and common assignment. Despite the fact that you aren't supposed to post homework on this forum, we still get a lot of it and for some reason, we still help people. I'm too much of a conformist to break convention so I'm still going to help you.
Now what you need to tell us (since we're already going out of our way by helping you with homework (or am I not supposed to say that either?)) is what's going wrong.
im sorry, i sounded like a jackass but im just pissed that i asked on another forum and i got insulted for typing the word help. and this manual that i have sucks rocks.
Last edited on
Let me rephrase my question...

how do i create a loop? and please explain it like i have lived in a cave my entire life.
This book is the size of like a super concentrated dictionary...maybe i should tell tide and they'll make 4x ultra detergent. lol. lame joke
Hey! Have u checked the tutorial on this site? http://www.cplusplus.com/doc/tutorial/control/
Hey! Have u checked the tutorial on this site? http://www.cplusplus.com/doc/tutorial/control/


well i kind of get the idea of using do-for loops but im gonna try to see how to get it to work for my program.

btw this isn't homework. i worked on version 1 of this cuz i was bored during last summer. i just stumbled upon it when i saw a channel called AntiRTFM on youtube.
Topic archived. No new replies allowed.