C program: reading chars then ints

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Program Reading
Click For Summary

Homework Help Overview

The discussion revolves around a C programming assignment that requires reading characters from standard input until an "=" is encountered, followed by reading the subsequent characters as an integer. Participants are exploring issues related to input handling and type-checking in their code.

Discussion Character

  • Exploratory, Problem interpretation, Assumption checking

Approaches and Questions Raised

  • Participants discuss the challenges of reading character values as integers, particularly when non-numeric characters are input. There is also a question about the expected behavior of the program when encountering characters instead of integers after the "=" sign.

Discussion Status

Some participants have offered guidance on handling input, including the use of functions like ungetc() to manage character input more effectively. Others are considering alternative methods such as reading into an array or using sscanf.

Contextual Notes

The assignment specifications are noted to be incomplete, prompting participants to seek clarification from the instructor regarding expected behavior and additional requirements.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
The assignment states: "Write a code fragment that reads and discards all characters from standard input until it encounters a "=", and then reads the following characters as an int."

I have a couple of problems:
1) reading char values as int, for instance if I input abc=123 there's no problem, but if I input abc=abc, then there's trouble. I was thinking the ASCII value would be read and stored, but it's not happening.

2) the instruction "reads the following characters as an int". If I input ABC=ABC, should the program only end up printing the value of C as the integer?

Here's what I have so far - I am including the whole program. Thanks for your advice.

#include <stdio.h>
int main(void)
{
char mychar;
int myint;

while((mychar = getchar())!= '=')
{
continue;
}

scanf("%d", &myint);
printf("%d", myint);

return 0;
}
 
Physics news on Phys.org
It seems your program meets the specification. The specification does not instruct you to do any kind of type-checking on the characters themselves to see if they are decimal numerals.

Although it seems your program meets the spec, the spec is admittedly incomplete (it would be unacceptable in industry!). If I were you, I'd call the professor and make sure (s)he didn't intend to provide any more specifics.

- Warren
 
Thanks, Warren. I will email my prof about this and see if I can get more clarification.
 
New specs

My teacher said if I wanted a challenge I should code the assignment to accept either an integer or a character after the '=' sign. I thought maybe I could write this so that if characters are entered I could display the ASCII value of the first one entered. But now I am getting stuck since I used scanf() to read an integer I don't know how to handle a character being entered.
thanks.

#include <stdio.h>
int main(void)
{
char mychar;
int myint;

while((mychar = getchar())!= '=') //discard all until = is read
{
continue;
}
if (scanf("%d", &myint) == 1)
{
printf("That was an integer\n");
printf("It's value is %d\n", myint);
}
else
{
printf("That was NOT an integer\n");
printf("The ASCII value of the first character is...\n");
}
// now I am stuck
return 0;
}
 
The easiest way is to use ungetc() to push the character back onto the stream. That way you can "peek" at it, make a decision about it, then call scanf on the stream.

Alternatively, you can read your chars into an array and then use sscanf on the array.

- Warren
 
ungetc() huh? That's a new function for me. Cool- I am intrigued - I am going to play with that. Thanks!
I hadn't thought about using an array, either. We are just starting to work with those. hmmm...
 
Hrm, the istream classes have a "peek" function; stdio.h doesn't have an equivalent? :frown:
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K