something weird is happening. never seen before

Hey,

so i posted earlier about my ratio calculator and asked about how to prevent a character from trying to be written into an int/double variable

this is the code we came up with and it works perfect

1
2
3
4
5
6
7
8
9
 
if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			system("CLS");

			cout << "please enter a nurmeric value" << endl << endl;
		}


so after applying this bit of code everywhere that its needed, i set about trying to intentionally break the program any way i could. i could not do so, however i did run into a strage issue that i dont realy understand.

when entering a character into a numeric input, the program behaves as it should- it outputs a message saying that the input is wrong, and loops back to the input request.

now to understand the issue i kinda have to explain the sequence of things, ill also drop the code for it below this paragraph. the sequence is:

-item name request,
-item value request,
-*if correct input type, carry on to next stage (either input another items info, or readback and confirmation of inputed info)
-*if incorect input type loop back up to item name request

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
int infograb()
{

	for (; counter != Numberofratiovariables;)
	{

		cout << "please enter the name of item " << counter + 1 << "." << endl;
		cin >> itemname[counter];

		cout << endl << endl << "please enter the value of " << itemname[counter] << "." << endl; 
		cin >> b;
		
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			system("CLS");
			cout << "Please only enter numeric values for your item value." << endl << endl;
		}


		else
		{
			value[counter] = b;
			counter = counter + 1;
			system("CLS");
		}

	}

	system("CLS");

	return 0;

}


however if there is more then one character inputted, i.e.- gg, the first g is cleared and ignored, and the program loops. however the second g is automatically used as the item name, and the loop starts again at the -item value request position. its as if the second g isnt being cleared and is then being used as the name of the item. this also occurs if i enter a numeric value with a character at the end- i.e. 400g. the program dose not loop as it should, the 400 is used as the value, and the g is automatically entered as the name of the next item.

im assuming this is because i have to add some parameters to the cin.clear(*add parameters here?*)? if thats the case, can someone explain cin.clear() to me? like whats the syntax for adding parameters?

this is litteraly the very last hurdle before i build the program
Last edited on
im assuming this is because i have to add some parameters to the cin.clear(*add parameters here?*)?

Did you find and read any documentation for this standard function?
http://www.cplusplus.com/reference/ios/ios/clear/

if thats the case, can someone explain cin.clear() to me me?

Do you know that this is the reason documentation for this function exists?

By the way if you had found and read the documentation for this function you would have found that the normal way of calling this function is without any parameters. And you would have found out that all this function does is clear the stream error flags, nothing else.

Your problem is not with clear(), but probably with the ignore() function. Have you found and studied any documentation for this function, if not I suggest you do so. You need to remove all the offending characters from the stream not just the first character found.


yes i do know that the reason the documentation exists is exactly to answer questions like mine. im sorry if im coming off as lazzy and unwilling to help my self, thats not the case- 90% of the issues i had with this project i resolved on my own by researching on my own.

i find these documents very difficult to follow along with and to understand, not to mention i have a great deal of trouble navigating the database to find what i need and typically just get frustrated very quickly and give it up as a bad job.

however, not wanting to waste anyone time with questions that i realize have probably been asked a million times before, or if not are likely tedious to answer, ill try to find a solution on my own.

thank you for pointing out that issue is likley not with clear().
There are more than one location for documentation for standard functions and classes, perhaps you need to see about finding a source that you can understand. Because until you can understand the documentation for the standard functions and classes you will have an extremely difficult time in learning to program.

And remember that search engines can help as well, don't be afraid to type a short description of your problem into the search bar and see where that leads.


ok so from what i've researched this is how cin.ignore() works

cin.ignore( *number of character you wish to ignore*, *or up to specific character type you wish to ignore*)?

so for example cin.ignore(500, ' ') would ignore all characters up to 500 or up to the first space?

what if i want to clear the entire cin buffer? the space that it will clear up to is in the code, or in the input thats been entered? what if i dont have any spaces? or what if i cant predict what character i want it to clear up to? we're dealing with user input after all, it could be anything. what if i cant predict how many characters will need to be ignored? again, its user input, it could be an infinit amount of characters. the stuff i was reading mentioned adding '/n' as the second parameter, but didnt realu explain what '/n' means. will this tell it to clear upto the characters / and n? or dose this have some sort of specific meaning? can i just use cin.ignore(1000), which in my mind is telling it to ignore 1000 characters? what if theres only 3 characters, will it then also in\gnore the next 997 that may be entered afterwards?

*sorry, just noticed your latest replay, can you recomend any other databases? ill try to find some others on my own. it just seems like all the databases that explain this tuff use some sort of syntax that im not framiliar with, thats the biggest issue for me
Last edited on
so for example cin.ignore(500, ' ') would ignore all characters up to 500 or up to the first space?

Correct. However this can often leave characters in the input buffer.

what if i want to clear the entire cin buffer?

Do you realize that the input buffer is line buffered? This means that the last character in the input buffer is the end of line character. Do you know the character sequence that represents this character?

its user input, it could be an infinit amount of characters

No, there is a maximum number of characters that a stream can hold. You may want to view this pinned topic since it shows how to use the ignore() function to properly clear the input buffer.
http://www.cplusplus.com/forum/beginner/1988/

Also you ask what '/n' means but what you really mean is '\n'. The '\n' character sequence is called a control sequence. The '\' is called the escape character, it signifies that the next character is to be processed as a special control character. Here is a sample of some documentation for escape sequences.
https://www.codingunit.com/cplusplu-tutorial-constants-escape-codes-and-strings

can i just use cin.ignore(1000), which in my mind is telling it to ignore 1000 characters? what if theres only 3 characters, will it then also in\gnore the next 997 that may be entered afterwards?

Yes, but then the ignore() function will need to consume everything until 1000 characters have been consumed, probably not what you want.

*sorry, just noticed your latest replay, can you recomend any other databases?

Sorry, but since I have no idea what sources you find so confusing, no I can't be of much help here. But if you type in the name of some standard function you will probably get thousands of links to information about that function. For example "C++ ignore()" produces over 15,000,000 links when using the Google search engine.

Topic archived. No new replies allowed.