?
EDIT: Oh; I guess that you mean that you want user to input uint and you want to get it into variable. Well, you can use cin.get(), <cstdio>'s functions...
this is an assignment i was given for school and that is the requirement. we have not gone over how to format this. i am trying to get ahead.
we always use
using namespace std.
but i am sure what you are saying would work giblit.
MatthewRock
this is the questionrequirements.
Designing the “ReadInt” function, it should be value returning and have a reference parameter. The function needs to process one character at a time from the input buffer and deal with it in a correct fashion. Once the number has been read in, then make sure the input buffer is empty, otherwise the loop in main may not work correct. I know this is not how the extraction works, but lets do it this way.
Boy, are they teaching you this code? It's a bit terrible.
Main should return int.
You don't need additional curly brackets inside main. It just limits scope, but you don't need that.
And your function, from what I see, should take reference to some integer, then extract number from stream, and assign the number to the reference.
What other ways of getting input, other then cin>>, have you learned? I guess it may either require you to look in book/web to find proper function, or use one you've already learned.
Either case, answer question(according to requirements): what C++ function allows you to read single character from stream?
I don't see it saying he can't use cin >> it just looks like they want to extract one character at a time. Via cin >> or cin.get reading into a character instead of integer then converting.
1) Read in an int.
2) Check if the stream failed if so clear,ignore,display error and ask for a new input.
3) Check if the number is greater than or equal to zero and less than or equal to 65535
4) You now have a proper input.
pseudo code:
1 2 3 4 5 6 7 8
do
ask user to enter a number between 0 and 65535
get user input
check if it was valid input (!cin == failed)
if(failed)
clear, ignore, display error, and continuewhile number < 0 or number < 65535
output the number
#include <iostream>
#include <stdlib.h>
usingnamespace std;
int ReadInt (unsignedshortint & N);
int main()
{
int Error = 0;
char Ch, Repeat;
longint Number = 0, N = 0;
do
{
cout << "\n\nEnter a Number --> ";
cin >> Ch;
if (!isdigit(Ch))
{
Error = 1;
return Error;
}
while (isdigit(Ch))
{
Number = ((Ch - '0') + Number);
Ch = cin.get();
if (Ch != '\n')
Number = Number * 10;
}
if (Number > 65535)
Error = 2;
cout << "\n\nThe number inputed is " << Number ;
cout << "\n\nError Code " << Error;
cout << "\n\n Would you like to use program again? Y or N? ";
cin >> Repeat;
}
while (Repeat != 'n' || Repeat != 'N');
}
i need to split these into two functions.
ErrorCheck, and ReadInt.
i am just having trouble understanding the question of the function that needs to be made.
write a function that will read an unsigned integer into a variable of type unsigned short int