How to limit integer and how to detect key press

i'm on first year of IT study and we haven't started programming so i took matters in to my own hands so everything i know was self thought...sooo please don't be mad for writing ugly and unreadable code i did my best. Questions are in the code (C++)

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



using namespace std;

float main()
{
	
	char again = 'y';
	while (again == 'y')
		// how to do this
		//if(button R pressed)
		// {
		// R--
		// }

	{
		//coordinates
		float X, Y, Z;
		int R = 0; //how to limit R to 0 and 1
                  if (R < 0)     |
		  {              |
		 	R++;     | //does this work ?
		  }              |     
		   if (R > 1)    |
		  {              |
		  	R--;     |
		  }



		cout << "whats your x \n ";
		while (!(cin >> X))
		{//1.0 loop
			cout << "this is not an number \n";
			cin.clear();
			cin.ignore(100, '\n');
		}//1.1
		cout << "whats your y \n ";
		while (!(cin >> Y))
		{//2.0 loop
			cout << "this is not an number \n";
			cin.clear();
			cin.ignore(100, '\n');
		}//2.1
		cout << "whats your z \n ";
		while (!(cin >> Z))
		{//3.0 loop
			cout << "this is not an number \n";
			cin.clear();
			cin.ignore(100, '\n');
			R++;
		}//3.1
		if (R = 1)
		{
			while (!(R = 0))
			{
				cout << X;
				cout << Y;
				cout << Z;
				//how to sleep???
			}
		}

		else
		{
			cout << "change?(y/n) \n ";
			cin >> again;
		}



	} //end a loop
	cout << "end\n";
	return 0;

}
  
Last edited on
Hello tiger0998,

Welcome to the forum and thank you for using code tags.

Not bad for an early attempt. I straightened up the code a bit. The indenting here is a little more than what I see in my IDE, but you get the idea. I made comments in the code of what you could change or where there is a problem. I noticed that you used "float"s, just so you know "double"s are preferred most of the time.

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

//using namespace std;  // <--- Best not to use.

int main()  // <--- only needs to be an int.
{
	char again = 'y';
	while (again == 'y')
	{  // Beginning 1st while loop. <--- Added.
		// how to do this
		//if(button R pressed)
		// {
		// R--
		// }

		//{  // <--- If this is for the 1st while loop it is in the wrong place otherwise not needed.
			//coordinates
			float X, Y, Z;
			int R = 0; //how to limit R to 0 and 1 <--- A Bool would only be 0 or 1.

			// <--- For a single line the {} are not needed.
			if (R < 0)  // <--- "=" would be a better choice. Or "<=".
			{
				R++; 
			} 

			if (R > 1)  // <--- "=" would be a better choice. Or ">=". 
			{ 
				R--;
			}

			std::cout << "whats your x \n " << std::endl;  // <--- Output would look better with the "std::endl"

			while (!(cin >> X))
			{//1.0 loop
				cout << "this is not an number \n";
				cin.clear();
				cin.ignore(100, '\n');
				// <--- Should prompt for a new number. Also for "y" and "z".
			}//1.1

			std::cout << "whats your y \n " << std::endl;

			while (!(cin >> Y))
			{//2.0 loop
				cout << "this is not an number \n";
				cin.clear();
				cin.ignore(100, '\n');
			}//2.1

			std::cout << "whats your z \n " << std::endl;

			while (!(cin >> Z))
			{//3.0 loop
				std::cout << "this is not an number \n" << std::endl;
				std::cin.clear();
				std::cin.ignore(100, '\n');
				R++;
			}//3.1

			if (R = 1)
			{
				while (!(R = 0))
				{
					std::cout << X;
					std::cout << Y;
					std::cout << Z;
					//how to sleep???
				}
			}

			else
			{
				std::cout << "change?(y/n) \n ";
				std::cin >> again;
			}
		//} //end a loop <--- If thi is the end of the 1st while loop it would be alright otherwise not needed.
	}  // End 1st while loop <--- Added.

	std::cout << "end\n";

	return 0;
}


I will load up the program and see what I get when I compile the program.

Hope that helps,

Andy
Hello tiger0998,

After thinking on your questions this is what I see:

Starting at line 12 you want to know how to check if "R" has been pressed. My question to you is what platform is the program being written for? Is it Windows, some type of UNIX or Mac? This will make a difference in how you check for the key press being the letter "R". Because each platform is different.

Also at the beginning of the while loop you are wanting to check if "R" has been pressed, but you have done nothing that requires "R" to be pressed yet.

For the next question starting at line 22. Consider this if "R = -10" the if statement would change that to -9 not what you want. And for the second if statement if "R = 10" you would end up with 9 for "R". This might be closer to what you want:

1
2
3
4
5
6
7
8
9
if (R < 1)
{
	R = 0; 
} 

if (R > 1)  // <--- "=" would be a better choice. Or ">=". 
{ 
	R = 1;
}


After line 38 consider a cout statement to prompt the user for the new input instead of a blank screen, so to speak, waiting for something. And the same for the while loops for "Y" and "Z".

Line 57 creates an endless loop because the is no change to "R".

Line 61 could use a "\n" or "endl", so everything does not run together.

Line 75 you might want to write that as: "\nend\n". The first "\n" just some extra space before "end" so it is easier to read.

After working with this program maybe you could describe what the program is supposed to do and what you want it to do.

Hope that helps,

Andy
you can sometimes force-fit integers without if statements using %.
x%= 10; //x will be from 0 to 9.

there are also std::max and min

x = max(x, 0); //x will be x if > 0 and 0 if it was negative

these can make your intentions clearer and the code simpler for most bounded cases (where x is allowed to be in some range).

Topic archived. No new replies allowed.