how can i make function that can read an unsigned integer into a variable of type unsigned short int

How can i write a function that will read an "unsigned integer" into a variable of type "unsigned short int"?
 i can not use cin >> inside the function
.. so i am looking for atleast a hint!
Last edited on
You mean this:
1
2
3
4
void funct(unsigned short int a)
{
  //code...
}


?
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...
Last edited on
I don't see the purpose of reading into an unsigned long then assigning it to an unsigned short.
i can not use cin >>
Is there a reason why you can't? You could always use std::getline then parse with a std::stringstream
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.



I do not get this at all..

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int ReadInt (unsigned short int & N);
int main()

{
    int Error = 0;
    char Ch;
    long int Number = 0;
    long int N;
    
    cout << "Enter a Number --> ";
    cin >> Ch;
    
    if (!isdigit(Ch))
        {
            
        Error = 1;
        return Error;
            
        }
    while (isdigit(Ch))
        {
            
        Ch = Ch - '0';
            
            
        }
    if (Number > 65535)
        Error = 2;
    
    else
        
        N = Number;
    
    
    return Error;
    
        
}

Last edited on
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.
this is what i have been coming up with. i am not sure how to
write a function that will read an unsigned integer into a variable of type unsigned short int


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
#include <iostream>
#include <stdlib.h>

using namespace std;

int ReadInt (unsigned short int & N);
int main()

{
    int Error = 0;
    char Ch;
    long int Number = 0;
    long int N;
    
    cout << "Enter a Number --> ";
        cin >> Ch;
    
    if (!isdigit(Ch))
        {
            
        Error = 1;
        return Error;
            
        }
    while (isdigit(Ch))
        {
            
        Number = Ch - '0';
            
        }
    if (Number > 65535)
        Error = 2;
    
    else
        
        N = Number;
    
    
    return Error;
    
        
}
How about this.

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 continue
    while number < 0 or number < 65535
    output the number

Here is my updated code

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int ReadInt (unsigned short int & N);
int main()

{
    int Error = 0;
    char Ch;
    long int Number = 0, N = 0;
    
    cout << "Enter a Number --> ";
        cin >> Ch;
    
    if (!isdigit(Ch))
        {
            
        Error = 1;
        return Error;
            
        }
		while (isdigit(Ch))
        {

		Number = ((Ch - '0') + Number);
		Ch = cin.get();
		if (Number < 65535)
		Number = Number * 10;

        }

    if (Number > 65535)
        Error = 2;
    
    else
        
        N = Number;
    
    
    return Error;
    
        
}
Last edited on
how can i write a function that will read an unsigned integer into a variable of type unsigned short int?
Last edited on
how can i make it so when user enters 45678 that i will not get 456780?

also i am still working on function. i would greatly appreciate some feedback.
Is the assignment making you read in with characters instead of integers directly?
in the algorithm given there were characters used and it has thrown me off but
i have found how to make the number through a formula without using cin >>. 


the assignment however just shows that this needs to be done.

   write a function that will read an unsigned integer into a variable of type unsigned short int


i have made a loop to obtain the number through the characters.

1
2
3
4
5
6
7
8
9
		while (isdigit(Ch))
        {

		Number = ((Ch - '0') + Number);
		Ch = cin.get();
		if (Ch != '\n')
		Number = Number * 10;

        }


i need to write the function to do the actual problem. any feedback would be helpful.

Last edited on
Here is my updated code

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




#include <iostream>
#include <stdlib.h>

using namespace std;

int ReadInt (unsigned short int & N);
int main()

{
    int Error = 0;
    char Ch, Repeat;
    long int 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
Last edited on
It sounds like they want you to just read into an unsigned short.

So
1
2
3
4
unsigned short input;
std::cout << "Please enter an unsigned integer: ";
std::cin >> input;
//you can error check here if it failed to read. 
im not sure what you are telling me.
can you please elaborate.
I figured it out. thanks for trying to help.
Last edited on
Topic archived. No new replies allowed.