3 Replies - 270 Views - Last Post: 22 January 2013 - 05:32 PM Rate Topic: -----

#1 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Program only executing first input.

Posted 22 January 2013 - 04:18 PM

#include <conio.h>
#include <iostream>
#include <cctype>


int main()
{
	 char A, C, B, D;

	printf("Press a printable key >");
	scanf("%c", &A);
	printf("%c's integer equivalent is %d", A, (int) A);

	while(A != '#');
	
	printf("Press a printable key >");
	scanf("%c", &C);
	printf("%c's integer equivalent is %d\n", C, (int) C);

	while(C != '#');
	
	printf("Press a printable key >");
	scanf("%c", &B)/>/>/>;
	printf("%c's integer equivalent is %d", B, (int) B)/>/>/>;

	while(B != '#');

	printf("Press a printable key >");
	scanf("%c", &D);
	printf("%c's integer equivalent is %d", D, (int) D);
	
	while(D != '#');

	getch();
	return(0);
}


Haha me again :P/>/> So... this code kinda works... If I run it I can enter any keyboard character and get its correct ASCII code, but I can only do it once. It runs through the first printf()scanf()printf() then will go no further. I don't understand what is wrong with it to not proceed to the next printf() and let me enter another character.

Thanks for any help!

*** Edit ***
Please use code tags when posting code
:code:

This post has been edited by GunnerInc: 22 January 2013 - 07:23 PM
Reason for edit:: Added elusive code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Program only executing first input.

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: Program only executing first input.

Posted 22 January 2013 - 04:37 PM

"while(A != '#');" is an infinite loop. Let's say I enter 'x'. 'x'!='#' so we execute the code.....except you dont specify any code so we just keep repeating the empty while loop. I dont understand what you are trying to accomplish with the loops so I would suggest removing them. If you want to do something with the loops, this is the correct format.

while(A != '#')
{
  //code...
  //code...
  //code that changes A...
}


Was This Post Helpful? 0
  • +
  • -

#3 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Program only executing first input.

Posted 22 January 2013 - 04:46 PM

I am just trying to get ASCII integer equivalents for any character I put in on the keyboard. Are those loops not necessary for that? And did you place the loops above int main() or did you put them after in main() and before the {?

And removing them let me put in first input, but when i pressed enter to receive output, it game me output and the printf's of the C variable one then let me put in character for B variable set and gave me the right ASCII output but also gave me the next set as well without letting me put in an input.

So its letting me enter every other one and doing its own thing with the other two
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Program only executing first input.

Posted 22 January 2013 - 05:32 PM

That is because the Carriage Return is still in the input buffer as the result of you pressing the Enter key. Your first scanf gets the letter you pressed and the second scanf gets the Carriage Return.

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1