i cant get the digit nor the letter that the user entered to print to

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 335 Views - Last Post: 06 February 2018 - 03:04 PM Rate Topic: -----

#1 lonewolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:19 PM

as stated i am having a problem getting the digit and the letter variables to print after the user enters them only the float is printing.

#include <stdio.h>
#include <stdlib.h>


int main()
{
	
	
int user_given_digit;
float user_given_decimal;
char user_given_letter;


	// get the  whole number  and pass it to user_given_digit;
	 printf("Can I get you to enter a whole number please.\n");
	 scanf("%i", &user_given_digit);
	 
	// get the decimal  number  and pass it to user_given_decimal;
	 printf("Can I get you to enter a decimal number please.\n");
	 scanf("%f", &user_given_decimal);
	
	// get the  letter  and pass it to user_given_letter;
	 printf("Can I get you to enter a letter please.\n");
	 scanf("%d", &user_given_letter);
	
	
	
	printf ("Thank you for your entries .  \n");
	// print back what the user entered ;
	printf ("%You have entered %.1f, %.2f, %.3f.", user_given_digit, user_given_decimal,   user_given_letter);	
	
	
	
	return 0;
}



can anyone tell
me what im doing wrong?
thank you

P.S. i dont seam to be putting the tags [] around the code properly. can someone temm me what im doing wrong there also.
thanks

This post has been edited by Skydiver: 06 February 2018 - 12:18 PM
Reason for edit:: Fixed botched code tags. Thanks for trying.


Is This A Good Question/Topic? 0
  • +

Replies To: i cant get the digit nor the letter that the user entered to print to

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6011
  • View blog
  • Posts: 20,684
  • Joined: 05-May 12

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:20 PM

Look closely at line 30. Why are you using %f for the different types of data?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13768
  • View blog
  • Posts: 54,954
  • Joined: 12-June 08

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:21 PM

%.1f, %.2f, %.3f

Have you checked out the list of specifiers?

Quote

f Decimal floating point,

http://www.cplusplus.../cstdio/printf/

Perhaps use different ones for a character and integer?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6011
  • View blog
  • Posts: 20,684
  • Joined: 05-May 12

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:22 PM

As noted in your other thread, to put code in code tags, you have the following options:
- Press the [ CODE ] button on the toolbar. Between the [ code ] and [ /code ] paste in your code.
- Type in [ CODE ] (without the spaces), paste in your code, and the type in [ /code ] (without the spaces).
Was This Post Helpful? 1
  • +
  • -

#5 lonewolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:25 PM

when i use the %c it wont even let me enter a letter it will skip that entry

View PostSkydiver, on 06 February 2018 - 12:22 PM, said:

As noted in your other thread, to put code in code tags, you have the following options:
- Press the [ CODE ] button on the toolbar. Between the [ code ] and [ /code ] paste in your code.
- Type in [ CODE ] (without the spaces), paste in your code, and the type in [ /code ] (without the spaces).





THANK YOU!
Was This Post Helpful? 0
  • +
  • -

#6 lonewolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:31 PM

ok i can now get the digit and the float to print correctly but not the letter. this is what i changed
from line 30 %.1d, %.2f, %.3c. thank you to modi123_1!

but why will the %c not work? im confused!!
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13768
  • View blog
  • Posts: 54,954
  • Joined: 12-June 08

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:41 PM

Let's see your current code.
Was This Post Helpful? 0
  • +
  • -

#8 lonewolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:55 PM

View Postmodi123_1, on 06 February 2018 - 12:41 PM, said:

Let's see your current code.

[/
#include <stdio.h>

int main(void)
	// I prefer to put all of my opening and closeing brackets on a line by themselfes,and tab every thing inbetween them, it makes it easier to see if they have corasponding brackets.
	{
	    int firstNumber = 10;
	    double secondNumber = 15.25; // wouldn't float work here also?

	    printf("\nWelcome to IT2240 - \"Introduction to Programming\"\n");
	    printf("This is a sample C program. ");
	    printf("The two variables are integers:\n");
	    printf("\tfirstNumber = %d\n", firstNumber);
	    printf("\tsecondNumber = %f\n", secondNumber);

	    double sum = firstNumber + secondNumber;// this adds the two numbers together and passes it to the sum varpable
	    printf("The sum of firstNumber + secondNumber is %.2f.\n", sum);
	    printf("The average of the numbers is %.3f.\n\n", sum / 2);

		/*  If you use a much more descriptive name for variables, one that tells what tit does, then you won't need to comment nearly as much.
		And if someone comes by later and changes the code without changing the comment then the comment is now a misleading lie.
		That is all too often the way it works in the real world unfortunately.
		*/
	    int myNumber;// Here instead of my number I would use "user_imputed_number"
	    printf("Enter an integer: ");// I'd also add a check to make sure that a number was entered. as it is i can enter a letter without an error though it does say that I entered 0(zero)
	    scanf("%d", &myNumber);
	    printf("You entered %d.\n", myNumber);
	    return 0;
	}
]
:code:

This post has been edited by modi123_1: 06 February 2018 - 12:56 PM
Reason for edit:: In the future use the [code] button in the editor.

Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13768
  • View blog
  • Posts: 54,954
  • Joined: 12-June 08

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 12:57 PM

... that doesn't look like the first post.. nor does it look to be asking for character input.
Was This Post Helpful? 0
  • +
  • -

#10 lonewolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 01:02 PM

View Postmodi123_1, on 06 February 2018 - 12:57 PM, said:

... that doesn't look like the first post.. nor does it look to be asking for character input.

i ammended the post but here it is again and osrry for the wrong code.
[/

#include <stdio.h>
#include <stdlib.h>


int main()
{
	
	
int user_given_digit;
float user_given_decimal;
char user_given_letter;


	// get the  whole number  and pass it to user_given_digit;
	 printf("Can I get you to enter a whole number please.\n");
	 scanf("%i", &user_given_digit);
	 
	// get the decimal  number  and pass it to user_given_decimal;
	 printf("Can I get you to enter a decimal number please.\n");
	 scanf("%f", &user_given_decimal);
	
	// get the  letter  and pass it to user_given_letter;
	 printf("Can I get you to enter a letter please.\n");
	 scanf("%c", &user_given_letter);
	
	
	
	printf ("Thank you for your entries .  \n");
	// print back what the user entered ;
	printf ("%You have entered %.1d, %.2f, %.3c.", user_given_digit, user_given_decimal,   user_given_letter);	
	
	
	
	return 0;
}


]
:code:

This post has been edited by modi123_1: 06 February 2018 - 01:04 PM
Reason for edit:: In the future use the [code] button in the editor.

Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13768
  • View blog
  • Posts: 54,954
  • Joined: 12-June 08

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 01:05 PM

Dude.. seriously. Select your code and click the [ code] button in the editor to add code tags. What ever you think you are doing is not working.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13768
  • View blog
  • Posts: 54,954
  • Joined: 12-June 08

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 01:11 PM

Think about your input. What's in the hopper after someone enters "1.2"? The enter key! A new line! That qualifies as a character, right?
Was This Post Helpful? 0
  • +
  • -

#13 lonewolf71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-February 18

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 01:12 PM

View Postmodi123_1, on 06 February 2018 - 01:05 PM, said:

Dude.. seriously. Select your code and click the [ code] button in the editor to add code tags. What ever you think you are doing is not working.

i got it figured out now sorry for the inconvenience.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13768
  • View blog
  • Posts: 54,954
  • Joined: 12-June 08

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 01:14 PM

As it is.. perhaps flush that input buffer after a scanf.

 while ((getchar()) != '\n');

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon

Reputation: 5398
  • View blog
  • Posts: 16,829
  • Joined: 25-December 09

Re: i cant get the digit nor the letter that the user entered to print to

Posted 06 February 2018 - 01:15 PM

You don't seem to realize that the "%c" format specifier doesn't skip leading whitespace by default. You can force skipping the whitespace by placing a space in front of the specifier: " %c".


Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2