issue with reference/RVDT function

Pages: 12
Nov 19, 2014 at 2:20am
I don't have any syntax errors, but when I enter a number between 0 and 65536 I expect the function to return a value of 0 for error, but it keeps returning 1. I am convinced that I have a tiny retarded error that I cannot find.

The code asks for a user to input a number then returns one of 3 error codes, if error 0 then the number the user entered is also returned. I know the structure may not be the most efficient means of solving the problem, but I am working within the confines of a very strict assignment.

http://pages.iu.edu/~mhollowa/c101/homework10.pdf

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>
using namespace std;

int ReadInt(int &N);
void main()
{
	char Answer; 
	int Number, Error; 

	cout<< "Start Y or N? ";
	cin>> Answer;
	while (Answer != 'N')
	{
		cout<< "Enter a Number \n";
		cin>> Number;
		Error = ReadInt(Number);
		if (Error == 1)
			cout<< "Illegal entry \n";
		else if (Error == 2)
			cout<< "Overflow error \n";
		else
		{cout<< Number;}

		cout<< "Continue Y/N? ";
		cin>> Answer;
	}
}

int ReadInt(int &N)
{   
	char Ch;
	long int X = 0;
	cin.get(Ch);
	if ((Ch < '0') || (Ch > '9'))
	{
		return 1;
	}
	while (isdigit(Ch))
		{
			X = (X*10) + Ch;
			cin.get(Ch);
		}
	if (X > 65535)
	{
		return 2;
	}
	else
	{
		N = X;
		return 0;
	}
	
}
Last edited on Nov 19, 2014 at 3:08am
Nov 19, 2014 at 2:37am
Why are you asking for the number twice? On line 15 and line 33, you ask for a number.
Last edited on Nov 19, 2014 at 2:40am
Nov 19, 2014 at 2:39am
I ask for a number, but then I am using cin.get(ch) to extract one character at a time from the number input.
Last edited on Nov 19, 2014 at 2:43am
Nov 19, 2014 at 2:44am
Mixing cin and cin.get is, to my knowledge, not good, because cin actually leaves the newline character in the stream, so your cin.get reads the stream, which is '\n'. You can flush the stream with an endl or a cin.ignore(), then you should be able to continue using both.
Last edited on Nov 19, 2014 at 2:45am
Nov 19, 2014 at 2:46am
Sorry, I am very new to C++. Are you sure cin.get() will work that way? When I tried your code, it just set Ch to int value of 10 (which is a newline).
Nov 19, 2014 at 2:46am
You should use the modulus operator to extract one character at a time from an int.
I think:
char ch = (N %10);

Also, why pass N as reference?
Last edited on Nov 19, 2014 at 2:48am
Nov 19, 2014 at 2:47am
Where would you recommend I place the cin.ignore exactly?
Nov 19, 2014 at 2:49am
After each of your cin statements. Not sure if that will work, but try anyway.
Last edited on Nov 19, 2014 at 2:49am
Nov 19, 2014 at 2:51am
iLiberate,

I would approach it differently. I would take the input as a string, and then convert each number individually to int.
Nov 19, 2014 at 2:55am
If you mean to take the Answer variable as a string instead of a char, I agree. But the int should stay an int; taking a number as a string unnecessarily complicates things.

iLiberate, then all your cin statements can be getline, which discards the null operator when it's done reading.
Last edited on Nov 19, 2014 at 2:56am
Nov 19, 2014 at 2:58am
I just want everyone to understand that this problem is unnecessarily difficult by design, not by my choice.
Last edited on Nov 19, 2014 at 2:59am
Nov 19, 2014 at 3:00am
I'm just a beginner to CPP, but how else will he traverse through each number of something like 2389218398291839 ?
Nov 19, 2014 at 3:08am
I posted a link to my homework assignment so everyone can understand the constraints.
Nov 19, 2014 at 3:28am
You need a to have a comparison against 65535, so if (input >65535) cout an error message. That should take care of your 'illegal' numbers.

Use the getline function as noted above, which skips leading whitespace (1). Then, as your input is now a string, you can access it like array elements. So, take in a number, then do something like:
1
2
if(!isdigit(string[0]))
return error;

(http://www.cplusplus.com/reference/cctype/isdigit/)
to find out if the first character is a number (2)

Then you can iterate through the elements in the string that are numbers and process them(3).

Using that isdigit() function will allow you to check the value and you can then break out if any input is not numeric(4)
Nov 19, 2014 at 3:31am
Your assignment says to get the input one value at a time, so you may have to resort back to using a char instead of a string, but then you can still use the cin.get function:

http://www.cplusplus.com/reference/istream/istream/get/
Nov 19, 2014 at 3:33am
We haven't used strings or istream in class yet though, so I imagine that isn't the way he wants me to go about it.
Nov 19, 2014 at 3:44am
Okay, don't do strings, do char instead like you originally had. And the get function is just in the istream class. You have indeed used them in class, since your original program has cin.get in it (from the istream class). Click on the link and read the page. An example is at the bottom.
Nov 19, 2014 at 3:46am
The real problem here is that you aren't converting the characters to an integer correctly. At line 40, Ch is an ASCII character representing a digit between 0 and 9. It's value is between 48 and 57 (the ASCII values for '0'-'9'. To convert from the ASCII value of a digit to it's numeric value, simply subtract '0':
X = (X*10) + (Ch - '0');
Nov 19, 2014 at 3:48am
When I debug, it is saying that 10 '\n' is the value of Ch, but how is that?
Last edited on Nov 19, 2014 at 4:09am
Nov 19, 2014 at 4:11am
Because 10 is the ASCII value of the NewLine character \n.
Pages: 12