Detecting Enter key

Hello, i searched in internet about how to detect enter key, yet my trials of getline(), cin.ignore() gave run-time error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  std::cout << "Define a value for a: ";
	double a;
	char q;
	

	std::cin >> a;
	
   
	
		
		std::cin >> q;
		
		
	
	if (q == '/') 
	{
		double w;
		std::cin >> w;
		a /= w;
	}


What i want to accomplish is, user can enter either in decimal form ( 10.12) and rational form(2/7).

Full code is here:
[spoiler]
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
83
84
85
86
87
88
89
90
91
92
93
94
95
// infinitesumtrial1.cpp: Konsol uygulamasının giriş noktasını tanımlar.
//

#include "stdafx.h"
#include <iostream>




double absolute(double x)
{
	if (x < 0)
		return -x;
	
		return x;
}

double infinitesum(double a, double r)
{
	long double epsilon = 1e-10;
	long double	sum = 0;
	double x = r;
	/*for (a, r; r < epsilon; r *= r)    //not working edit: lol becasuse r<epsilon

		sum += a;
	
	*/
	//double y= abs(r);
	while (absolute(r)> epsilon)
	{   
		sum += a * r;
		r *= x;
	}
		return sum;

}



int main()

{
	std::cout << "INFINITE SUM TRIAL 1 \n";
	std::cout << "Geometric Sum \n";

	std::cout << "Define a value for a: ";
	double a;
	char q;
	

	std::cin >> a;
	
   
	
		
		std::cin >> q;
		
		
	
	if (q == '/') 
	{
		double w;
		std::cin >> w;
		a /= w;
	}
	std::cout << "Define a value for r: ";
	double r;
	char t;
	
	std::cin >> r;
	std::cin >> t;
	
	if (t == '/')
	{
		double y;
		std::cin >> y;
		r /= y;
	}
	if (a == 0)
		std::cout << "The result is 0";

	if ((r >= 1 || r<= -1) && a != 0) {
		if (a > 0)
			std::cout << "The result goes towards positive infinity.\n";
			if (a < 0)
				std::cout << "The result goes towards negative infinity. \n";

	}
	if ((r<1 && r>0 ||r>-1 && r<0) && a!=0 )
		std::cout << "The result is: " << infinitesum(a, r)<<"\n";


    return 0;
}

[/spoiler]
Last edited on
Hi humman,

The problem is that when the user just types in 10.12, that number becomes 1 token put into your double a variable. But then it still attempts to get q. The solution has to be a bit more dynamic than this.

Although I don't see your attempt with getline, you can use it to grab the whole expression (either "10.2" or "2/7") and parse it yourself, appropriately.

I'm not sure if this is the simplest solution, but:
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

#include <iostream>
#include <string>
#include <stdexcept>      // std::invalid_argument

int main()
{
	std::string line;
	std::getline(std::cin, line);
	
	// if line contains a '/', split the two numbers between it.
	// otherwise, assume a decimal number
	size_t slash_index = line.find("/");
	if (slash_index != std::string::npos)
	{
	    // found a slash, take two substrings
	    std::string numerator = line.substr(0, slash_index);
	    std::string denominator = line.substr(slash_index+1);
	    
	    // convert string to int (then store it in a double)
	    try
	    {
	        double a = std::stoi(numerator);
	        double b = std::stoi(denominator);
	        double value = a / b;
	        
	        std::cout << "Parsed " << a << "/" << b << " = " << value << "\n";
	    }
	    catch (const std::invalid_argument& ia)
	    {
	        std::cout << "Invalid argument: " << ia.what() << "\n";
	        
	        // handle conversion error
	        
	    }

	}
	else
	{
	    // No slash found, assume it's a decimal (floating-point) number
	    try
	    {
	        double value = std::stod(line);
	        std::cout << "Parsed " << value << "\n";
	    }
	    catch (const std::invalid_argument& ia)
	    {
	        std::cout << "Invalid argument: " << ia.what() << "\n";
	        
	        // handle conversion error 
	    }
	}
}


10.12
Parsed 10.12
2/7
Parsed 2/7 = 0.285714
2/
Invalid argument: stoi
/2
Invalid argument: stoi


PS:
1
2
3
4
5
	if ((r >= 1 || r<= -1) && a != 0) {
		if (a > 0)
			std::cout << "The result goes towards positive infinity.\n";
			if (a < 0)
				std::cout << "The result goes towards negative infinity. \n";

This indentation is misleading.
It should be formatted as
1
2
3
4
5
6
	if ((r >= 1 || r<= -1) && a != 0) {
		if (a > 0)
			std::cout << "The result goes towards positive infinity.\n";
		if (a < 0)
			std::cout << "The result goes towards negative infinity. \n";
	}
Last edited on
unrelated but there are nonstandard ways to trap the enter key, if you really, really need it. Most of the time, you don't. The issue appears if you try to write your own version of, for example, the simple windows program 'pause' (which many people put in programs to freeze the screen, but it was designed for batch file programming I believe). It is extremely difficult to recreate that program using only official c++ commands, and its a 1 line statement using extensions.
Topic archived. No new replies allowed.