Passing string in functions

Hello,

I am learning about pointers/references. My program is asking user for input(string) and I am trying to use this input in different functions throughout my program.

I successfully printed the string and its memory location but now I want to use this string in an int function but I cannot because they are not the same type?

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
#include <iostream>
#include <string>


using namespace std;

string inputDen()
	
	{
		string s;
		
		cout << "Please enter denominator: " << endl;						// INPUT FOR DENOMINATOR
		getline(cin, s);
		cout << endl;
			
		for(int i = 0; i < s.length(); i++) {							// For length of string, check if not between 0 - 9
			if (! (s.at(i) >= '0' && s.at(i) <= '9'))
				{
					cout << "Invalid input, enter numbers only " << endl;
					cout << "Please enter denominator: " << endl;
					getline(cin, s);
				
				}
			else	
					return s;		
			
		}
	}
	

string inputNum()

	{
		string s;

		cout << "Please enter numerator: " << endl;						// INPUT FOR NUMERATOR
		getline(cin, s);
		cout << endl;
		
		for(int i = 0; i < s.length(); i++) {							// For length of string, check if not between 0 - 9
			if (! (s.at(i) >= '0' && s.at(i) <= '9'))
		       		{	
					cout << "Invalid input, enter numbers only " << endl;
					cout << "Please enter numerator: " << endl;
					getline(cin, s); 	
				}
		
			else
				return s;
		}
	}

bool to_lowest_terms(int &numerator, int &denominator) // I want to pass my user input to here
	
	{
		
		return true;

	}



int main() {
	
	string numerator = inputNum(); 		// pass user input to string
	string denominator = inputDen();

	cout << "Numerator value: " << numerator << endl; 
	cout << "Denominator value: " <<denominator << endl << endl;
	cout << "Numerator address: " << &numerator << endl;
	cout << "Denominator address: " << &denominator << endl << endl;
	
	return 0;
}
Last edited on
My program is asking user for input(string)

Why a string? You appear to need some kind of numeric type, so why not just use the proper numeric type?

Good question.

I believe I want to because I want to include decimals and negative numbers.
Would you still recommend I use an int return rather than a string?
Would you still recommend I use an int return rather than a string?

Not an int if you want decimals, but perhaps a double would be better than a string.

Thank you!!
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
#include <iostream>
#include <string>

double get_input(std::string& question);
bool to_lowest_terms(double&, double&);

int main()
{
   std::string question { "Enter the numerator: " };
   double numerator   { get_input(question) };

   question = "Enter the demoninator: ";
   double denominator = get_input(question);

   std::cout << "Numerator value: " << numerator << '\n';
   std::cout << "Denominator value: " << denominator << "\n\n";
   std::cout << "Numerator address: " << &numerator << '\n';
   std::cout << "Denominator address: " << &denominator << "\n\n";
}

double get_input(std::string& question)
{
   double input { };

   std::cout << question;
   std::cin >> input;
   std::cout << '\n';

   return input;
}

bool to_lowest_terms(double& numerator, double& denominator)
{
   // do you source voodoo here

   return true;
}

Enter the numerator: 12

Enter the demoninator: 5

Numerator value: 12
Denominator value: 5

Numerator address: 012FFA0C
Denominator address: 012FFA04
Topic archived. No new replies allowed.