hi, I have this code:

#include<iostream>
#include<cstdio>
#include<sstream>
using namespace std;
int main (char argc)
{
       float n1;
       char operators;
       string n2;
       float n2_act;
       float ans;


       cin >> n1;

       for(;;)
       {
                 cin >> operators;
                 cin >> n2;
                 
                 
                 if(n2 == "k") 
                 {
                            continue;
                  }
                  else
                  {
                            stringstream(n2) >> n2_act;
                            if(operators == '+')
                            {
                                     n1 = n1 + n2;
                             }
                  }
         }
     cout << n1 << endl;
}

I don't get an answer when I enter 1+2+3 and when I put cout << n1 << endl; inside the for() loop, the answer comes out as 3. Any solutions?

Edited 5 Years Ago by thecoolman5: n/a

Output your values at key points in your code to
1) make sure the variables contain the values they should
2) make sure the IF is processing the correct code

i made some adjustments,

#include<iostream>
#include<sstream>
using namespace std;
int main (char argc)
{
    float n1, n2_act;
    char operators;
    string n2;
    int t = 0;
    
    for(;;)
    {
           if(t == 0)
           {
                cin >> n1;
           }
           
           cin >> operators; //i want it to declare operators as @ and if there isnt anymore input by the user after the user enters the equation, have the program skip the cin and have operators stay as @.
           cin >> n2;
           
           if(operators == '@')
           if(n2 == "k")
           {
                 break;
           }
           else
           {
                 stringstream(n2) >> n2_act;
                 
                 if(operators == '+')
                 {
                              n1 = n1 + n2_act;
                              operators = '@';
                              n2 = "k";
                              t = 1;
                 }
           }
    }
    cout << n2 << endl;
    t = 0;
}

i have it add the first number and the second number if the operator is + and have the program declare operators = '@' and n2 = "k" so if there isnt anymore things to cin, then have the program exit the loop. I dont know if it works but how would you make the program automatically enter its own input?

//small example
cin >> n2 = "k"; //then the compiler know that n2 = "k" so the user doesnt have to enter the input

Edited 5 Years Ago by thecoolman5: n/a

i dont really know how to explain this but ill try my best,

#include<iostream>
#include<sstream>
using namespace std;
int main (char argc)
{
    for(;;)
    {
    float n1, n2_act;
    char operators;
    string n2;
    int t = 0;
    //you enter 1+2+3
    for(;;)
    {
           if(t == 0)
           {
                cin >> n1;//this takes the 1
           }
           
           cin >> operators;//this takes the +, then after the equation, this takes the +, since there isnt anymore operators that the user inputed, skip this cin
           cin >> n2;//this takes the 2, and this takes the 3, since there isnt anymore numbers, skip this cin
           
           if(operators == '@')//operators equals +, operators = +, now operators = @
           if(n2 == "k")//n2 equals 2, n2 = 3, now n2 = "k"
           {
                 break;//it goes to the cout << n2 << endl;
           }
           else
           {
                 stringstream(n2) >> n2_act;//this takes the 2 and puts it in n2_act;, then it does this part all over again
                 
                 if(operators == '+')
                 {
                              n1 = n1 + n2_act;//equals 3, equals 6
                              operators = '@';//operators = @, operators = @
                              n2 = "k";//n2 = "k", n2 = "k"
                              t = 1;//t = 1 so you dont have to enter n1 again, n1 equals the answer of the original equation, n1 will keep acting as the answer for future equations
                 }
           }
    }
    cout << n2 << endl;
    t = 0;//it resets t so you can enter another equation
}
}

study it carefully and see if you can help. Thanks

Is there a requirement behind what you're doing? Seems like you're making this harder than it needs to be ... maybe you could provide more details on what your problem definition is?

I don't need to study it carefully. I already know how to help. And I did but you obviously didn't like it. Too bad, because it will work great towards answering your questions.

i figured it out

#include<iostream>
using namespace std;
int main (char argc)
{
    for(;;)
    {
    int l;
    cout << "Enter how many operators is your equation is going to have?" << endl;
    cin >> l;
    float n1;
    char operators;
    float n2;
    int t = 0;
    cout << "Enter equation." << endl;
    cout << "Please put spaces between each item and this calculator does not use\nthe mathematical order."<< endl;
    cin >> n1;
    for(l = l;l > 0; l--)
    {
           if(t == 0)
           {
                
           }
           
           cin >> operators;
           cin >> n2;
           
           if(operators == '+')
           {
                        n1 = n1 + n2;
           }
           
           if(operators == '-')
           {
                        n1 = n1 - n2;
           }
           
           if(operators == '/')
           {
                        n1 = n1 / n2;
           }
           
           if(operators == '*')
           {
                        n1 = n1 * n2;
           }
           
    }
    cout << n1 << endl;
    system("pause");
    system("cls");
}
}

now, how would you make it detect how many variables that your equation has?

Edited 5 Years Ago by thecoolman5: Needed &quot; at the end of order

how would you do that? I tried declaring int c = 0; and after you input an operator, c is increased, and replace for(l = l;l > 0; l--) with for(int c = 0; c > 0; c--) but that just messes up the loop and the program doesnt run properly.

how would you do that? I tried declaring int c = 0; and after you input an operator, c is increased, and replace for(l = l;l > 0; l--) with for(int c = 0; c > 0; c--) but that just messes up the loop and the program doesnt run properly.

Answer #1) Then think about it longer and try again.

Answer #2) And that's was supposed to tell me what you did and I'm supposed to be able to tell you how to fix your code from this description?

Answer #3) Then try something else that will work.

Edited 5 Years Ago by WaltP: n/a

ok, ive tried like 50 times and i can't figure it out. Heres my code:

#include<iostream>
using namespace std;
int main (char argc)
{
    for(;;)
    {
    float n1;
    char operators;
    float n2;
    int t = 0;
    int c = 0;
    cout << "Enter equation." << endl;
    cout << "This calculator does not use the mathematical order." << endl;
    cin >> n1;
    cout << " ";
    for(c = c;c > 0; c--)
    {
           
           cin >> operators;
           c = c + 1;
           cout << " ";
           cin >> n2;
           cout << " ";
           system("cls");
           cout << n1;
           cout << operators;
           cout << n2 << endl;
           
           if(operators == '+')
           {
                        n1 = n1 + n2;
                        c = c + 2;
           }
           
           if(operators == '-')
           {
                        n1 = n1 - n2;
                        c = c + 2;
           }
           
           if(operators == '/')
           {
                        n1 = n1 / n2;
                        c = c + 2;
           }
           
           if(operators == '*')
           {
                        n1 = n1 * n2;
                        c = c + 2;
           }
           
    }
    cout << n1 << endl;
    system("pause");
    system("cls");
}
}

You said to insert a counter when I enter a variable. I did that and it doesn't work. All I need to do is detect how many operators the user imputed.

Your code will not enter the inner for() loop because you have c = 0 right before it and your condition is when c > 0 .

Try explaining what is wrong -- in detail.

You said to insert a counter when I enter a variable. I did that and it doesn't work. All I need to do is detect how many operators the user imputed.

And that's explaining the problem in detail? I think the first thing you need to understand is what detail means.

Your code will not enter the inner for() loop because you have c = 0 right before it and your condition is when c > 0 .

So how would you write it?

You're kidding, aren't you?

Sit down with your instructor and tell him you are having problems understanding why the code STFU referenced is wrong. He's the one you need to take these little problems to. We are a help forum, not a teaching forum and you need to be taught some very very basic concepts. Your instructor is the best one to do that.

Walt why are you so hostile all the time? I could sit here all day calling people stupid because some of them have no clue what they are doing.

If you think they are retarded don't reply to the post.

Comments
When you get another 6000 posts and 3 additional years, let's see where you are.

ok i have an idea. The user inputs the equation as a string. The program calculates the position of the first operator in the string. Have the program stringstream the beginning of the string to the position of the operator. The only problem is i dont know how to get the program to stringstream a string from a certain point to another point. Do you guys know how? If you can give me working code, then i should be able to finish the program by myself.

here guys, this a finished version of a normal calculator that can handle paranthesis, order of operations, and unlimited operators.

#include<iostream>
#include<string>
#include<sstream>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
int main ()
{
    cout << "No variables." << endl;
    cout << "Type in cls to clear the screen, and type exit to exit." << endl;
    SetConsoleTitle("Calculator");
    for(;;)
    {
    re:
    string equation = "";
    string equation2 = "";
    string equation3 = "";
    string equation4 = "";
    string equation5 = "";
    int non_digit5 = 0;
    int non_digit2 = -1;
    int non_digit6 = 0;
    int c = 0;
    int i = 0;
    float n1 = 0;
    string operators = "";
    float n2 = 0;
    cin >> equation;
    DWORD ticks = GetTickCount();
    if(equation == "exit")
    {
                return 0;
    }
    
    if(equation == "cls")
    {
                system("cls");
                goto re;
    }
    while(true)
    {
               c = equation.find('(', c);
               if(c == -1)
               {
                    break;
               }
    //PARENTHESIS SOLVING CODE
    do
    {
                  non_digit2 = equation.find('(', non_digit2+1);
                  if(non_digit2 == -1)
                  {
                        break;
                  }
                  non_digit5 = non_digit2;
    }while(non_digit2 != -1);
    non_digit5 = non_digit5 + 1;
    int non_digit3 = 0;
    //DOUBLE PARANTHESIS EQUATIONS
    equation4 = equation.substr(non_digit5, 999999999);
    int l = equation.length();
    int l1 = equation4.length();
    non_digit3 = equation4.find(")", non_digit3+1);
    l1 = l1 - non_digit3;
    l = l - l1;
    non_digit3 = l;
    equation3 = equation.substr(non_digit3+1, 999999999);
    equation2 = equation.substr(non_digit5, non_digit3);
    do
    {
                  non_digit2 = equation2.find(')', non_digit2+1);
                  if(non_digit2 == -1)
                  {
                        break;
                  }
                  non_digit6 = non_digit2;
    }while(non_digit2 != -1);
    equation2 = equation2.substr(0, non_digit6);
    equation = equation.substr(0, non_digit5-1);
    n1 = 0;
    operators = "";
    n2 = 0;
    int non_digit = equation2.find_first_not_of("12345677890.");
    string part = equation2.substr(0, non_digit);
    stringstream(part) >> n1;
    operators = equation2[non_digit];
    equation2 = equation2.substr(non_digit + 1);
    while(true)
    {
             n2 = 0;
             non_digit = equation2.find_first_not_of("1234567890.");
             part = equation2.substr (0, non_digit);
             stringstream(part) >> n2;
             if(int(non_digit) == 0)
             {
                               string part = equation2.substr (0, non_digit+3);
                               stringstream(part) >> n2;
                               equation2 = equation2.substr(non_digit + 1);
             }
        if(operators == "+")
        {
                     n1 = n1 + n2;   
        }      
        
        if(operators == "-")
        {
                     n1 = n1 - n2;
        }
        
        if(operators == "*")
        {
                     n1 = n1 * n2; 
        }
        
        if(operators == "/")
        {
                     n1 = n1 / n2;
        }
        
        if(non_digit == -1)
        {
                           break;
        }
        non_digit = equation2.find_first_not_of("0123456789.");
        operators = equation2[non_digit];
        equation2 = equation2.substr(non_digit + 1);
    }
        stringstream out;
        out << n1;
        equation = equation + out.str();
        equation = equation + equation3;
    }
    //ORDER OF OPERATIONS
    while(true)
    {
               c = equation.find_first_not_of("1234567890.-+");
               if(c == -1)
               {
                    break;
               } 
    equation2 = "";
    equation3 = "";
    equation4 = "";
    operators = "";
    float n1 = 0;
    float n2 = 0;
    float n3 = 0;
    int non_digit = equation.find_first_not_of("1234567890.-+");
    operators = equation[non_digit];
    equation2 = equation.substr(non_digit+1, 999999999);
    stringstream(equation2) >> n1;
    equation3 = equation.substr(0,non_digit);
    reverse(equation3.begin(), equation3.end());
    //BEGIN FIXING REVERSED EQUATION
    non_digit = equation3.find_first_not_of("1234567890.");
    if(non_digit == -1)
    {
                 reverse(equation3.begin(), equation3.end());
                 stringstream(equation3) >> n2;
    }
    else
    {
                 equation5 = equation3.substr(0, non_digit);
                 reverse(equation5.begin(), equation5.end());
                 stringstream(equation5) >> n2;
    }
    
    stringstream out;
    out << n2;
    equation4 = equation4 + out.str();
    equation4 = equation4 + operators;
    stringstream out1;
    out1 << n1;
    equation4 = equation4 + out1.str();
    //END OF FIND OPERATOR
    int l = equation4.length();
    non_digit = equation.find(equation4);
    equation2 = equation.substr(0, non_digit);
    equation4 = equation.substr(non_digit+l, 999999999);
    if(operators == "/")
    {
                 n3 = n2 / n1;
    }
    if(operators == "*")
    {
                 n3 = n2 * n1;
    }
    stringstream out2;
    out2 << n3;
    equation2 = equation2 + out2.str();
    equation2 = equation2 + equation4;
    equation = equation2;
    }
    //START FIXED EQUATION SOLVING
    n1 = 0;
    operators = "";
    n2 = 0;
    size_t non_digit = equation.find_first_not_of("12345677890.");
    string part = equation.substr (0, non_digit);
    stringstream(part) >> n1;
    operators = equation[non_digit];
    equation = equation.substr(non_digit + 1);
    while (true)
    {
             n2 = 0;
             size_t non_digit = equation.find_first_not_of("1234567890.");
             string part = equation.substr (0, non_digit);
             stringstream(part) >> n2;
             if(int(non_digit) == 0)
             {
                               string part = equation.substr (0, non_digit+3);
                               stringstream(part) >> n2;
                               equation = equation.substr(non_digit + 1);
             }
        if(operators == "+")
        {
                     n1 = n1 + n2;   
        }      
        
        if(operators == "-")
        {
                     n1 = n1 - n2;
        }
        
        if(operators == "*")
        {
                     n1 = n1 * n2; 
        }
        
        if(operators == "/")
        {
                     n1 = n1 / n2;
        }
        
        if (int(non_digit) == -1)
        {
                           break;
        }
        
        non_digit = equation.find_first_not_of("0123456789.");
        operators = equation[non_digit];
        equation = equation.substr(non_digit + 1);
    }
    cout << "Speed of calculation: " << GetTickCount() - ticks << " milliseconds." << endl;
    cout << "Result : " << n1 << endl;
    system("pause");
    goto re;
    }
}
This article has been dead for over six months. Start a new discussion instead.