getchar() not responding in C program

getchar() gets char first run through while loop but not for second ru

Page 1 of 1

6 Replies - 1177 Views - Last Post: 22 May 2009 - 11:18 PM Rate Topic: -----

#1 matrixaffiliate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-April 09

getchar() not responding in C program

Post icon  Posted 22 May 2009 - 10:27 AM

I'm trying to write a simple C program for practice that asks for a char and based on what char you enter the program will either ask the question again or exit. This is my code:

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

char ch;
int repeat;

int main() 
{
	repeat=1;

	while(repeat==1){
			
		printf("To continue enter 1.  To quit enter 0. ");
		ch = getchar();
			
		if (ch=='1')
			repeat=1;
		else
			repeat=0;
		}
	return 0;
}




The code compiles fine. However when I run it, the program accepts the char the first time, but when it repeats the while loop it won't accept a char in command prompt and exits the while loop and program. The code looks legit, but I'm not sure because I've only been using C for about 5 weeks now.

Any help is greatly appreciated.

Thank you!
Matrixaffiliate

Is This A Good Question/Topic? 0
  • +

Replies To: getchar() not responding in C program

#2 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: getchar() not responding in C program

Posted 22 May 2009 - 11:10 AM

Try this.
	while(repeat==1){
		   
		printf("To continue enter 1.  To quit enter 0. ");
		ch = getchar();
		cin.ignore(100, '\n');
   
		if (ch=='1')
			repeat=1;
		else
			repeat=0;
		}



Was This Post Helpful? 0
  • +
  • -

#3 matrixaffiliate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-April 09

Re: getchar() not responding in C program

Posted 22 May 2009 - 11:33 AM

Isn't cin a C++ command only? I tried it and it wouldn't compile.

These were the errors:

error C2065: 'cin' : undeclared identifier
error C2224: left of '.ignore' must have struct/union type

This post has been edited by matrixaffiliate: 22 May 2009 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: getchar() not responding in C program

Posted 22 May 2009 - 12:00 PM

ignore is a c++ function. To flush the buffer in C, use while((ch=getchar())!='\n' && ch !=EOF);
Was This Post Helpful? 0
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: getchar() not responding in C program

Posted 22 May 2009 - 12:04 PM

I suppose that is why C++ is easier, you don't have to type out all that madness....IDK, C is a great thing to learn though, i'm gonna learn it one day when I have time.

This post has been edited by IngeniousHax: 22 May 2009 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 matrixaffiliate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-April 09

Re: getchar() not responding in C program

Posted 22 May 2009 - 12:22 PM

Thanks a million Blake! It works like a charm now! :D :w00t:

And IngeniousHax I strongly suggest that you learn C. I started out with Java at school and though it was a good language to start with, it didn't really teach me what programming was. C++ started to introduce me to what I was really doing when I programmed, but C has taught me why and how I did things in Java and C++ that were found in libraries and such. It's really made me appreciate programming more and to put more thought into my programs. C makes you a better programmer. Taking the time to learn it is totally worth it! :D :tup:

Thanks again to everyone who posted!
Matrixaffiliate
Was This Post Helpful? 0
  • +
  • -

#7 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: getchar() not responding in C program

Posted 22 May 2009 - 11:18 PM

Yeah I definitely plan to learn C, it's not something im just gonna brush off to the side, it's just currently I don't have the time to do so, studying for finals and all that jazz, and next semester im gonna be carrying something around 20 units so I don't think I will have the time to study up on some C while taking intermediate C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1