Help with C Programming using getchar() line please?!

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Hi guys. I am writing a program for class and there is one thing I just can't figure out. I have to write a program that reads input until encountering #. Then it prints each character and the ASCII decimal code. The problem is that I can do the whole enter input until # is entered using getchar, but the how do I return each INDIVIDUAL character!??! Thanks!

I am using the following for user input:
while ((ch = getchar ()) != '#)
 

Red and black

Member
Apr 14, 2005
152
0
0
Well, at least you admit you're getting help on a homework assignment. That's more honest than most such requests.
 

blustori

Senior member
Mar 2, 2005
753
0
0
ugh... thats reading in characters until it reaches end of line. you need to read until you reach a '#' so you want to != '#'
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Originally posted by: blustori
ugh... thats reading in characters until it reaches end of line. you need to read until you reach a '#' so you want to != '#'
Yea sorry, it was just a mistake, but do you know how I can return each character entered besides just returning ch?
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Well what are you doing now to store the characters you're reading in?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
I'd help, but it would be to lead you through the process of learning.

Usually what happens in these threads is someone just posts an answer, the student takes it and scampers off having learned nothing, and goes on to fail the final.
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Well the characters are being stored in ch. Here's what I got so far:

int main(void)
{

char ch;
printf("Enter some character and # to terminate");
while ((ch = getchar ()) != '\n')
{
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: Techie333
Well the characters are being stored in ch. Here's what I got so far:

int main(void)
{

char ch;
printf("Enter some character and # to terminate");
while ((ch = getchar ()) != '\n')
{

that's *all* you have so far? Did you even look at your textbook or take notes? I can't see how a professor would require this without at least going over some of the basics.
 

blustori

Senior member
Mar 2, 2005
753
0
0
dude you are going to fail. char c only takes in one character so what you have previously stored is replaced by the next character. you need an array.
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Originally posted by: BigJ
Originally posted by: Techie333
Well the characters are being stored in ch. Here's what I got so far:

int main(void)
{

char ch;
printf("Enter some character and # to terminate");
while ((ch = getchar ()) != '\n')
{

that's *all* you have so far? Did you even look at your textbook or take notes? I can't see how a professor would require this without at least going over some of the basics.
That's not all I have, that's how far I can get in the program. I can obviously do the rest of the program if I learn the single line that returns individual characters
 

Techie333

Platinum Member
Jan 20, 2001
2,368
0
0
Originally posted by: blustori
dude you are going to fail. char c only takes in one character so what you have previously stored is replaced by the next character. you need an array.
Hi, you are wrong, it takes in the entire line.
 
Jun 4, 2005
19,723
1
0
Originally posted by: Techie333
Originally posted by: blustori
dude you are going to fail. char c only takes in one character so what you have previously stored is replaced by the next character. you need an array.
Hi, you are wrong, it takes in the entire line.

But your program isn't working, right?
I think the point is made...
 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
Originally posted by: Techie333
Originally posted by: blustori
dude you are going to fail. char c only takes in one character so what you have previously stored is replaced by the next character. you need an array.
Hi, you are wrong, it takes in the entire line.

Hi, you are wrong, getchar returns a single character.
 

blustori

Senior member
Mar 2, 2005
753
0
0
Originally posted by: Techie333
Originally posted by: blustori
dude you are going to fail. char c only takes in one character so what you have previously stored is replaced by the next character. you need an array.
Hi, you are wrong, it takes in the entire line.
I know it reads the entire line, but after each character is read, the previous character is lost.

try this:

#include <stdio.h>
int main(void)
{
int a;
char b = '#';
printf("Dude~ you are going to fail your programming class.\n");
for(a = 0;; a++)
if(b == '#')
printf("oh noes!!!\n");
return 0;
}
 

newbiepcuser

Diamond Member
Jan 1, 2001
4,474
0
0
found this using getchar() example, try this example and see if leads you in the right direction. I believe its using "C"

 

CrimsonChaos

Senior member
Mar 28, 2005
551
0
0
I'm with the other folks here, I'm not going to write code for your class... but I can offer some explanation.

If you need to STORE each character for later use in your code, then you would use an "array". For example, if you want one line of output to be the alpha-numeric keystrokes of the input, then the next line to be the ASCII-equivalents, you'd likely want to use an array.

If each keystroke is going to trigger output (for example, the alpha-numeric input followed immediately by the ASCII-equivalent, then a newline), then you'd use putchar() in a while loop.

Regarding the ASCII decimal code, there's a C-function that will do that for you -- but I'm not going to tell you what it is, you'll have to find it in your text yourself. ;)

 

sao123

Lifer
May 27, 2002
12,653
205
106
Originally posted by: hardwareuser
C SUX... LEARN C++

C++ is like an extension of C, so I don't know what you're saying.


C without the C++ extensions is like having a penis without a girl to put it in... all your left with is your hand.