Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a program that would find the area, length, radius or diameter of a circle depending on variables given by users. It will also round the number to a number of decimal places specified by the user (if needed).
The issue: at line 34 i need this to be the output "To decimal places or nearest X" where X is mm/cm/dm/m/km the user chose in the first function.

Here's my original code:

#include <iostream>
using namespace std;



void whatmeasure () {             /* a function to determine what units of measurment are being used*/
      string measure;
    cout << "mm = millimeters \n"   //answer options
    << "cm = centimeters\n"
    << "dm = decimeters\n"
    << "m = meters\n"
    << "km = kilometers\n"
    << "Your value is in: " ;     //the question 
    cin>> measure;                
    
}

void whatfind () {                            /*a function determining what the user wants to find*/
    char find;
    cout << endl << "r = radius of the circle\n" // answer options
    << "d = diameter of the circle\n"
    << "a = area of the circle\n"
    << "l = kreisumfang\n"
    << "You need to find: ";              // the question 
    cin >> find;                          

}

void howround () {                                /* the beginning of the function that detrmines if the answer needs rounding, and if yes, how.*/
     string answerneedround;
     cout << "Do you want to round the answer? (yes/no)\n";
     cin >> answerneedround;
     if (answerneedround == "yes") {
         cout << "To decimal places or nearest " <<measure; //THE PlACE OF DOUBT
     }
     
}

int main() {
    
    whatmeasure ();
    whatfind();
    howround ();
   

    return 0;
}


What I have tried:

I tried writing line 34 like this:
if (answerneedround == "yes") {
        cout << "To decimal places or nearest " << measure;
    }

BUT it shows an error and says that "measure" was not defined.
Why doesn't it accept what i tried? I defined "measure" at line 7! What should i do?
Thanks in advance)
Posted
Updated 30-Apr-23 21:45pm
v3
Comments
Member 15627495 1-May-23 3:19am    
hello !

define 'measure' in your 'main scope'. not elsewhere.

if you enclose a var init in a function, the use of this var keep located in the function itself.

reaching a var belong to 'where it was define'.

The best way would be to pass the values the function needs as parameters, and to have the other functions return them when they have been entered.

So whatmeasure would return a string (or better, it would return an enum value so they are always valid measures).
whatfind would return a char (or better another enum value)
howround would take the return value from whatmeasure and return an enum as well.

Then, when you call them, you store the values in local variables, and pass them to functions as needed.

THis makes you app flexible, and your methods reusable - which makes your code easier to read, easier to modify, and more reliable!
 
Share this answer
 
The variable measure is defined, and only exists, in the whatmeasure function. And the same with find and whatfind. So you need to change your functions to return their values, and change main to pass the relevant values to the functions. Somethoing like:
C++
#include <iostream>
using namespace std;



string whatmeasure () {             /* a function to determine what units of measurment are being used*/
      string measure;
    cout << "mm = millimeters \n"   //answer options
    << "cm = centimeters\n"
    << "dm = decimeters\n"
    << "m = meters\n"
    << "km = kilometers\n"
    << "Your value is in: " ;     //the question 
    cin>> measure;                
    
    return measure; // *** pass the value back to the caller
}

string whatfind () {                            /*a function determining what the user wants to find*/
    char find;
    cout << endl << "r = radius of the circle\n" // answer options
    << "d = diameter of the circle\n"
    << "a = area of the circle\n"
    << "l = kreisumfang\n"
    << "You need to find: ";              // the question 
    cin >> find;                          
    
    return find; // *** return the unit type
}

string howround (string measure) {                                /* the beginning of the function that detrmines if the answer needs rounding, and if yes, how.*/
     string answerneedround;
     cout << "Do you want to round the answer? (yes/no)\n";
     cin >> answerneedround;
     if (answerneedround == "yes") {
         cout << "To decimal places or nearest " <<measure; //THE PlACE OF DOUBT
     }
// you need to get the unit of rounding here

    return rounding; // *** return rounding value
     
}

int main() {
    
    string measurement = whatmeasure ();
    string tofind = whatfind();
    string roundingunit = howround (measurement);
   
// here you need to add the code to do the calculations

    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900