-1

I'm working on a project by where I have to create a simple program that works based of the user input. I've gone with a basic calculator but I'm having trouble getting my if/else if statements to work. Basically, if the user types in "Addition", I want the program to say "...I will help you with addition!", and so on for whether the user says "Subtraction", "Division", and "Multiplication".

I'm new to this and so this has already taken me hours upon hours, not looking for you to do it for me but to point out my erorrs and advise so that I can learn from it will be great.

TIA.

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace std;

//user inputs what he needs help with/program output
char Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
char inpsum[20];
cin >> inpsum;
char output;
if (inpsum == "Addition")
{
    cout << "Great! I'll help you with addition!" << endl;
}
else if (inpsum == "Subtraction")
{
    cout << "Great! I'll help you with subtraction!" << endl;
}
else if (inpsum == "Division")
{
    cout << "Great! I'll help you with division!" << endl;
}

else if (inpsum == "Multiplication")
{
    cout << "Great! I'll help you with multiplication!" << endl;
}


return 0; 

REST OF CODE

//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}

//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}

//division function
void Div()
{
    float div1, div2;
    cout << "Please enter two values you want divided" << endl;
    cin >> div1;
    cin >> div2;
    cout << "The answer is: " << (div1 / div2) << endl;
}

//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}



int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();

    return 0 ; 
}
d7994
  • 11
  • 1
  • 4
  • 1
    Just about every line in that piece of code is wrong. Where are you learning this stuff from? –  Mar 19 '17 at 21:47
  • The interwebs. It was working before I tried to add the if/else if for different outcomes lol. Tried to fix myself but couldn't n so am here – d7994 Mar 19 '17 at 21:49
  • 2
    You will not learn C++ from internet resources; you will need a good textbook. –  Mar 19 '17 at 21:51
  • 1
    I agree with Neil. You can find a list of recommended books [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Pandatyr Mar 19 '17 at 21:59
  • bit too late to get a book for this lmao but thanks – d7994 Mar 19 '17 at 22:01
  • Possible duplicate of [if..else statement in the c++ standard](http://stackoverflow.com/questions/24574238/if-else-statement-in-the-c-standard) – Oghli Mar 19 '17 at 22:16
  • Please, no need for hostility in the responses! The question was a bit clumsy, and it did not follow the "rubber ducky" principle of stackoverflow, so it will be naturally downvoted, but it is not constructive to belittle the asker. @d7994, the list of recommended books is indeed the best thing to follow right now. – Krastanov Mar 19 '17 at 22:38

2 Answers2

2

This code is all wrong you need to learn about C++ correctly first, here is the corrected code

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include<string>
using namespace std;

//addition function
float Add(float add1, float add2)
{

    return (add1 + add2);
}
//subtraction function
float Subt(float subt1, float subt2) {

    return (subt1 - subt2);
}

//division function
float Div(float div1, float div2)
{

    return (div1 / div2);
}
//multiplication function
float Mult(float mult1, float mult2)
{

    return (mult1 * mult2);
}
void input(float &num1, float &num2)
{
    cout << "\nEnter First Number : ";
    cin >> num1;
    cout << "Enter Second Number : ";
    cin >> num2;
}
//user inputs what he needs help with/program output
void Inpsum()
{
    cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
    float num1;
    float num2;
    string inpsum;
    cin >> inpsum;
    if (inpsum == "adding")
    { //if user enters "adding"
        cout << "Great!, I will help you with " << (inpsum) << endl;
        input(num1, num2);
        cout << "\nAnser Is " << Add(num1, num2);
    }//then output = "...i will help with adding"
    else if (inpsum == "subtraction") //otherwise, if user enters "subtraction"
    {
        cout << "Great!, I will help you with " << (inpsum) << endl; //then output = "...i will help with subtraction"
        input(num1, num2);
        cout << "\nAnser Is " << Subt(num1, num2);
    }
    else if (inpsum == "division") //if user enters "division" 
    {

        cout << "Great!, I will help you with " << (inpsum) << endl; ////then output = "...i will help with division
        input(num1, num2);
        cout << "\nAnser Is " << Div(num1, num2);
    }
    else if (inpsum == "multiplication") //if user enters "muliplication"
    {
        cout << "Great, I will help you with " << (inpsum) << endl; ////then output = "...i will help with multiplication"
        input(num1, num2);
        cout << "\nAnser Is " << Mult(num1, num2);
    }
    else
    {
        cout << "Enter A Correct Mathematical Operation";
    }
}
    int main()
    {
        Inpsum(); //user inputs what they want help with
        cout<<endl;
        system("pause");
        return 0;
    }
Mohammad Tayyab
  • 696
  • 4
  • 22
  • Thanks man, but I can't get it to run? http://prntscr.com/em0l2i - http://prntscr.com/em0lhy – d7994 Mar 19 '17 at 22:24
  • Screenshots above. "The system can not find the file specified" and two errors for unresolved externals. – d7994 Mar 19 '17 at 22:27
  • I edited code, but for this error you should change your project name or try this code in new c++ project. Your visual studio getting LNK error there is no problem in code. – Mohammad Tayyab Mar 19 '17 at 22:32
1

First of all, instead of using char array, use std::string.

Secondly, if-else statements have syntax errors.

Basic structure of if-else statements is like

if(condition)
{
   //code
}
else
if(condition)
{
  //code
}

More on if-else statements in C++

Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • Well, std::string. –  Mar 19 '17 at 21:53
  • I've updated the OP but it's totally ignoring "Great! I'll help you with ____". (I've probbaly done something wrong again) I also had an error with cout when trying to use std::string? – d7994 Mar 19 '17 at 22:26