Return Value

I recently started learning C++. I am going over a book and one thing that I am struggling little bit is return type functions. I am working on this program and my problem is that when i am trying to assign return value of selectShape() function to an int, instead of just assigning the value in the main function, it is also running the whole function all over again. How can I get the return value of selectShape() and simple assign to local variable inside the main function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

void welcome();
void getInfo (string &, string &);
int selectShape();
void getLenWid(double &, double &);
double calcSqFeet(double,double);
double getDiam();
double calcSqFeet(double);

const double PI = 3.14;

int main(){
    string name, telNumber;
    double len, wid;
    
    welcome();
    getInfo(name, telNumber);
    selectShape();
    
    int floorType = selectShape(); //contains the choice of the customer
    double diam = getDiam(); //contains the diameter of the circular floor
    
}

void welcome(){

    cout<<"\tWelcome to Concrete Pro, the best supplier of concrete floors in the country!\n";
    cout<<"\t\t\tWe have been providing our service for over 75 years!\n\t\t\t\tWe are not finished until you are satiffied!\n";
}

void getInfo(string &name, string &telNumber){
    cout<<"Please enter your full name: "<<endl;
    cin>>name;
    cout<<"Please enter your telephone number: (ex: 555.555.5555)"<<endl;
    cin>>telNumber;
}

int selectShape(){
    int select;
    cout<<"Estimate\n\n";
    cout<<"To describe the shape of the floor, please enter 1 or 2:"<<endl;
    cout<<"1. Rectangular\n2. Circular"<<endl;
    cout<<"Selection: ";
    cin>>select;
    
    if (select<1||select>2){
        cout<<"Please try valid input"<<endl;
    }
    return select;
}

void getLenWid(double &len, double &wid){
    cout<<"Please input the length of your floor:"<<endl;
    cin>>len;
    cout<<"Please input the width of your floor:"<<endl;
    cin>>wid;
}

double calcSqFeet(double len,double wid){
    double sqFeet = len * wid;
    return sqFeet;
}

double getDiam(){
    double diam;
    cout<<"Please enter the diameter of your floor:"<<endl;
    cin>>diam;
    return diam;
}

double calcSqFeet(double diam){
    double radius = diam/2;
    double sqFeet = PI * radius * radius;
    return sqFeet;
}
You do realise you're calling selectShape() twice in main?
yes I do, my question is are there ways to get the return value of selectShape() without running it twice. if you print floorType you will get the return value of selectShape()
Hi,

Delete line 24. It is not achieving anything there, because the return value isn't assigned to anything. Line 26 does what you want.

Also, std::cin only reads until it hits white space, so you need std::getline instead.
http://en.cppreference.com/w/cpp/string/basic_string/getline
thanks
Topic archived. No new replies allowed.