Question

I'm kind of new to programming, and I am having trouble figuring out why my program...

I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you.

Code:
#include <iostream>

using namespace std;

class Math
{
int num1, num2;

public:
void set_values(int, int);

int add()
{
int resultAdd;

resultAdd = num1 + num2;
return resultAdd;
}

int sub()
{
int resultSub;

resultSub = num1 - num2;
return resultSub;
}
};

void Math::set_values(int x, int y)
{
num1 = x;
num2 = y;
}

int main()
{
Math frstNum, secNum, result;
int choice;

cout << "Would you like to add or subtract?" << endl;
cout << "1: Add" << endl;
cout << "2: Subtract" << endl;
cin >> choice;

switch(choice)
{
case 1:
cout << "Enter first number to add: ";
cin >> frstNum; //The error starts at this line. I'm assuming that since the error starts here, it will keep giving me errors on every cin as well.
cout << "Enter second number to add: "; //Also, did I use the classes and objects correctly?
cin >> secNum;
result.set_values(frstNum, secNum);
cout << "Result: " << result.add() << endl;
break;
case 2:
cout << "Enter first number to subtract: ";
cin >> frstNum;
cout << "Enter second number to subtract: ";
cin >> secNum;
result.set_values(frstNum, secNum);
cout << "Result: " << result.subtract() << endl;
break;
default:
cout << "Sorry, you're stupid. =)" << endl;
break;
}

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Check the code below:

#include "stdafx.h"
#include <iostream>

using namespace std;

class Math
{
   int num1, num2;

public:
   void set_values(int, int);

   int add()
   {
       int resultAdd;

       resultAdd = num1 + num2;
       return resultAdd;
   }

   int sub()
   {
       int resultSub;

       resultSub = num1 - num2;
       return resultSub;
   }
  
};

void Math::set_values(int x, int y)
{
   num1 = x;
   num2 = y;
}

int main()
{
   Math result;
   int frstNum, secNum;
   int choice;

   cout << "Would you like to add or subtract?" << endl;
   cout << "1: Add" << endl;
   cout << "2: Subtract" << endl;
   cin >> choice;

   switch (choice)
   {
   case 1:
       cout << "Enter first number to add: ";
       cin >> frstNum; //The error starts at this line. I'm assuming that since the error starts here, it will keep giving me errors on every cin as well.
       cout << "Enter second number to add: "; //Also, did I use the classes and objects correctly?
       cin >> secNum;
       result.set_values(frstNum, secNum);
       cout << "Result: " << result.add() << endl;
       break;
   case 2:
       cout << "Enter first number to subtract: ";
       cin >> frstNum;
       cout << "Enter second number to subtract: ";
       cin >> secNum;
       result.set_values(frstNum, secNum);
       cout << "Result: " << result.sub() << endl;
       break;
   default:
       cout << "Sorry, you're stupid. =)" << endl;
       break;
   }

   return 0;
}

Please rate it if the above solution helps you in any way or if you have any concerns comment it, I will help you through again.

Add a comment
Know the answer?
Add Answer to:
I'm kind of new to programming, and I am having trouble figuring out why my program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...

    Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • So the assignment is to write a program that have the user entered 3 numbers and...

    So the assignment is to write a program that have the user entered 3 numbers and than it will sort it by ascending order. Here are the code I wrote, but it won't compiled and I kept on getting an error message "Using uninitiazed memory" for all the variables. Can someone please help take a look and see whats going on? #include <iostream> using namespace std; int main() { //variables int num1, num2, num3; int lowest, middle, highest; //user inputs...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • I am having trouble figuring out why my program will not make any columns, just rows....

    I am having trouble figuring out why my program will not make any columns, just rows. I need to display a square. The instructions are provided below. (This for my C++ class) Write a program that displays a square. Ask the user for the square’s size. Only accept positive integer numbers (meaning choose a data type that holds positive integer values). The size the user gives you will determine the length and width of the square. The program should then...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) {...

    Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: ";    cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2;    return 0; }

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT