Trouble with variables

closed account (DN7SE3v7)
In the code below, the first function gets input from the user and the second function is suppose to do some calculation and display an answer. However, I keep getting "-1.#NID" where the answer should be. Please help.

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
#include <iostream>
#include <string>
using namespace std;
double current_price, past_price;
void get_input(double current_price,double past_price);
void display_results(double current_price, double past_price);

int main()
{
	string answer;
    do
	{
	get_input(current_price, past_price);
	display_results(current_price, past_price);
	cout << "\n";
	cout << "Run the program again?(Type Yes or No.)\n";
        cin >> answer;
	}while(answer == "Yes" || answer == "yes");
}
void get_input(double current_price, double past_price)
{
    cout << "Please enter the current price of an item:\n";
    cin >> current_price;
    cout << "Please enter the past price of an item\n";
    cin >> past_price;
}
void display_results(double current_price, double past_price)
{
    double rate_of_inflation = (current_price - past_price)/past_price;
    cout << "The rate of inflation is: " << rate_of_inflation << "%\n";
    cout << "The estimated price in a year is: $" << (rate_of_inflation * current_price) + current_price;
}

while calling the get_input function, the arguement "current_price" and 'past_price' are not yet initilized(they don't exist yet). so you cant use them as arguement to call the function:

in do function

1
2
3
4
5
6
7
8
9
10
11
12
13
14

do
	{

	cout << "Please enter the current price of an item:\n";
    cin >> current_price;
    cout << "Please enter the past price of an item\n";
    cin >> past_price;
    display_results(current_price, past_price);
	cout << "\n";
	cout << "Run the program again?(Type Yes or No.)\n";
        cin >> answer;
	}while(answer == "Yes" || answer == "yes");




and delete the get_input function. should work like a charm.
closed account (DN7SE3v7)
One of the constraints of my assignment is to use a function to get the user input.
well, in that case you could use either a pointer or just function inside a function. I am just guessing that you haven't reached to pointer yet. so another way it is;

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
#include <iostream>
#include <string>
using namespace std;
double current_price, past_price;
void get_input();
void display_results(double current_price, double past_price);

int main()
{
	string answer;
    do
	{
	get_input();

	cout << "\n";
	cout << "Run the program again?(Type Yes or No.)\n";
        cin >> answer;
	}while(answer == "Yes" || answer == "yes");
}
void get_input()
{
    cout << "Please enter the current price of an item:\n";
    cin >> current_price;
    cout << "Please enter the past price of an item\n";
    cin >> past_price;
    display_results(current_price, past_price);
}
void display_results(double current_price, double past_price)
{
    double rate_of_inflation = (current_price - past_price)/past_price;
    cout << "The rate of inflation is: " << rate_of_inflation << "%\n";
    cout << "The estimated price in a year is: $" << (rate_of_inflation * current_price) + current_price;
}

oh, i didn't see that you had declared a global variable there. then it is easier without use of pointer:

in your original program, make these changes:

1
2
3
4
5
void get_input();///line number 5 in your original program

get_input(); /// in line 13;

void get_input()///in line 20 


that's it:
closed account (DN7SE3v7)
Thank you so much. Such a simple fix and I couldn't even see it.
Last edited on
@koopey
- they arent initialized, but they do exist. i dont know who told you they dont.
- you can use unitialized variables in an argument
- do is not a loop

@op: using namespace std; and global variables are bad. i suggest looking into passing by reference instead of global variables.
@Little Bobby Tales
- I meant to say that they don't contain the value that we want, just random number is stored in the memory of the variable. I used
they don't exist
in kind of literary sense to make it easier to understand and didn't realize 'exist' could be perceived in other ways.(which i now realize are true and more plausible)

- I don't understand how 'do' is not a loop(among others as while and for)

- continued program using global variable feeling that OP wanted it that way for sake of convenience.

- I didn't know using namespace std is bad practice. I use jumping into c++ and same is used in every of its sample program, at least till now.
im sorry... i meant do is not a function referring to:
in do function


same is used in every of its sample program, at least till now.

this is true unfortunately, but its very bad practice
Topic archived. No new replies allowed.