Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to make a calculator in c++? Am i doing it wrongly?

+16 votes
asked May 26, 2022 by INTAN NURATIQAH MIOR AWALUDDIN (220 points)
#include <iostream>
#include <string>

using namespace std;

int main()
int getvalue()

    string studentname, studentid, subjectname, subjectcode, groupproject, assignment1, assignment2, testscore1, testscore2, total ;
    char grade;
    cout<<"program to compute and student marks";
    for (int i=0; i<100; i++)
    int total = value1 + value2 + value3 ;
    //this program will run for 1 time and stop
        cout<<"\nEnter your name : ";
        getline(cin, studentname);
        cout<<"\nEnter your student ID : ";
        getline(cin, studentid);
        cout<<"\nEnter subject name : ";
        getline(cin, subjectname);
        cout<<"\nEnter subject code : ";
        getline(cin, subjectcode);
        cout<<"\nEnter assignment 1 marks : ";
        getline(cin, assignment1);
        cout<<"\nEnter assignment 2 marks : ";
        getline(cin, assignment2);
        cout<<"\nEnter test 1 marks : ";
        getline(cin, testscore1);
        cout<<"\nEnter test 2 marks : ";
        getline(cin, testscore2);
        cout<<"\nEnter group project marks : ";
        getline(cin, groupproject);
        cout<<"total mark is : "<<total<<endl;
    return 0;

3 Answers

0 votes
answered May 31, 2022 by Peter Minarik (89,230 points)
edited Jun 1, 2022 by Peter Minarik

Let's have a look at your code.

Your marking (assignment1, assignment2, testscore1, testscore2, total) is stored as an std::string. That data type is used to store text, not numbers. Try int instead to store numbers. You can read them like this:

std::cin >> mark1;

You also have some unexpected lines (garbage?) in your code. Please remove lines

int getvalue()


int total = value1 + value2 + value3;

as they do not belong there.

The problem of mixed input methods

Now your code compiles and handles numbers correctly if you applied the above fixes.

However, a new problem arose: sometimes you read input with std::cin >>, other times with getline(). I understand that getline() will read the whole line for you, spaces and all, while std::cin >> only read for the first space and this may be your desired behaviour. However, they handled whitespaces (including newline characters) differently and what std::cin >> leaves behind will be consumed by getline() and it may result in an empty input.

So a simple solution is to leave behind the benefit of getline() and use std::cin >>  all the time.

Another solution needs more work: use getline() all the time, but then scan your read line for numbers in it:

#include <string>
#include <sstream>

int readInt()
    std::string line;
    std::getline(std::cin, line);
    int number;
    std::stringstream(line) >> number;
    return number;

With the help of the above function, your code would look something like this:

assignment1 = readInt();

I hope this helps.

Good luck!

+1 vote
answered Jun 4, 2022 by SAHIL VARMA (160 points)
using namespace std ;
int main()
    char op;
    float num1,num2;
    cout<<"Enter the operand: +,-,*,/ ";
    cout<<"Enter two operands: ";
        cout<<num1<<" + "<<num2<<" = "<<num1 + num2;
        cout<<num1<<" - "<<num2<<" = "<<num1 - num2;
        cout<<num1<<" * "<<num2<<" = "<<num1 * num2;
        cout<<num1<<" / "<<num2<<" = "<<num1 / num2;
        //if the operator is other than: +,-,*,/, then the error message is shown;
        cout<<"Error! the operator is not correct";
return 0;

commented Jun 6, 2022 by Peter Minarik (89,230 points)
cout<<"Enter the operand: +,-,*,/ ";

Actually, that is the operator there, not the operand.

Otherwise, the code should work fine.

One could make it even better by checking for invalid inputs (e.g.: division by zero, not a number entered when needed) or changing the return code to non-zero when there was an error.
0 votes
answered Jun 29, 2022 by Joshua Mccarty (140 points)
All you have to do is

get the value of both numbers and add them.

include <iostream>

using namespace std;

int main


    int value1 = 0;
    cout << "VALUE 1" << endl;
    cin >> value1; //

    int value2 = 0;
    cout << "VALUE 2" << endl;
    cin >> value2; //

    int totalanswer = value1 + value2;

    cout << totalanswer << endl;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.