Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:36:05+00:00 2026-05-15T05:36:05+00:00

I have a 3 file program, basically teaching myself c++. I have an issue.

  • 0

I have a 3 file program, basically teaching myself c++. I have an issue. I made a switch to use the math function. I need and put it in a variable, but for some reason I get a zero as a result.

Also another issue, when I select 4 (divide) it crashes… Is there a reason?

Main file:

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

using namespace std;

int opersel;
int c;
int a;
int b;
string test;

int main(){

cout << "Welcome to Math-matrix v.34"<< endl;
cout << "Shall we begin?" <<endl;

//ASK USER IF THEY ARE READY TO BEGIN 

string answer;
cin >> answer;

if(answer == "yes" || answer == "YES" || answer == "Yes")
{

           cout << "excellent lets begin..." << endl;

           cout << "please select a operator..." << endl  << endl;


           cout << "(1) + " << endl;
           cout << "(2) - " << endl;
           cout << "(3) * " << endl;
           cout << "(4) / " << endl;

           cin >> opersel;

           switch(opersel){

                  case 1:
                  c = add(a,b);
                  break;
                  case 2:
                  c = sub(a,b);
                  break;
                  case 3:
                  c = multi(a,b);
                  break;
                  case 4:
                  c = divide(a,b);
                  break;
                  default:
                  cout << "error... retry" << endl;

                  }// end retry


           cout << "alright, how please select first digit?" << endl;

           cin >> a;

           cout << "excellent... and your second?" << endl;

           cin >> b;

           cout << c;

           cin >> test;

           }else if (answer == "no" || answer == "NO" || answer == "No"){


                 }//GAME ENDS








}// end of int main 

Here is my math.h file

#ifndef MATH_H
#define MATH_H

int add(int a, int b);


int sub(int a, int b);



int multi(int a, int b);


int divide(int a, int b);

#endif

Here is my math.cpp:

int add(int a, int b)
{

 return a + b;   

}

int sub(int a, int b)
{

 return a - b;   

}

int multi(int a, int b)
{

 return a * b;   

}

int divide(int a, int b)
{

 return a / b;   

}






}// end of int main 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:36:05+00:00Added an answer on May 15, 2026 at 5:36 am

    You’re calling your functions with a and b before you get the data from the user. Try saving the math function that they selected when they enter it, and move your switch to after you have asked them for a and b.

    #include <iostream>
    #include "math.h"
    #include <string>
    
    using namespace std;
    
    int opersel;
    int c;
    int a;
    int b;
    string test;
    
    int main(){
    
    cout << "Welcome to Math-matrix v.34"<< endl;
    cout << "Shall we begin?" <<endl;
    
    //ASK USER IF THEY ARE READY TO BEGIN 
    
    string answer;
    cin >> answer;
    
    if(answer == "yes" || answer == "YES" || answer == "Yes")
    {
    
               cout << "excellent lets begin..." << endl;
    
               cout << "please select a operator..." << endl  << endl;
    
    
               cout << "(1) + " << endl;
               cout << "(2) - " << endl;
               cout << "(3) * " << endl;
               cout << "(4) / " << endl;
    
               cin >> opersel;              
    
               cout << "alright, how please select first digit?" << endl;
    
               cin >> a;
    
               cout << "excellent... and your second?" << endl;
    
               cin >> b;
    
               switch(opersel){
    
                      case 1:
                      c = add(a,b);
                      break;
                      case 2:
                      c = sub(a,b);
                      break;
                      case 3:
                      c = multi(a,b);
                      break;
                      case 4:
                      c = divide(a,b);
                      break;
                      default:
                      cout << "error... retry" << endl;
    
               }// end retry
    
               cout << c;
    
               cin >> test;
    
               }else if (answer == "no" || answer == "NO" || answer == "No"){       
                     }//GAME ENDS   
    }// end of int main 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's not that easy. JPQL provides the IN operator for… May 16, 2026 at 12:12 pm
  • Editorial Team
    Editorial Team added an answer The problem is that R does not evaluate any expression… May 16, 2026 at 12:12 pm
  • Editorial Team
    Editorial Team added an answer Your script tag is pointing to the wrong URL: http://www.st-amz.com/content/images/jquery.cycle.min.js… May 16, 2026 at 12:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

How can Nginx serve crossdomain.xml file to a flash/flex program. Basically I need to
I need to create a .bat file to run java program. I have a
I have a gridview control in my c# program. Basically someone enters an email
I have a program that passes command-line arguments to an associated file (i.e. associated
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have an HTML file C:\Program Files\my_html_files\file.html . How do I open this file
I have a file containing a large number of occurrences of the string Guid=GUID
Im working with file systems and I have a List<> of file objects that
I have a folder containing .tcb and .tch files. I need to know what
I have a xml file that needs to be read from many many times.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.