Scanning a single character in c

This question already has an answer here:

  • Scanf skips every other while loop in C 10 answers

I have used the following code snippet to read several values but the single character constant variable rating is not accepting any value. It doesn't execute that particular scanf statement. How to resolve this?

char name[100],locality[100],vision[100],mission[100]; char rating; int dept,stud; printf("Enter college name\n"); scanf("%s",name); printf("Enter college locality\n"); scanf("%s",locality); printf("Enter college's vision\n"); scanf("%s",vision); printf(" Enter college's mission\n"); scanf("%s",mission); printf("Enter the number of departments\n"); scanf("%d",&dept); printf(" Enter student strength\n"); scanf("%d",&stud); printf(" Enter college rating\n"); scanf("%c",&rating); //not reading this value printf("%s\n%s\n",name,locality);

There is no compile error or run time error in this above code.

when I use the following code the variable is accepting value.

char rating; printf(" Enter college rating\n"); scanf("%c",&rating);

-------------Problems Reply------------

There is nothing wrong with your code except the printf statement of college's mission.

Category:c# Views:90 Time:2019-03-22
Tags: scanf char

Related post

  • Ways to search for a single character followed by a hyphen followed by more text using sql server full text search 2011-10-25

    We have data that might look something like: A-10001 A-10002 A-10003 B-10001 B-10002 B-10003 And where we'd like to find all of the matches containing "A-100". Using full text search, we are doing something like: where contains(e.*, '"A-100*"') Howev

  • Import text file as single character string 2012-01-30

    How do you import a plain text file as single character string in R? I think that this will probably have a very simple answer but when I tried this today I found that I couldn't find a function to do this. For example, suppose I have a file foo.txt

  • Output single character in C 2008-11-21

    When printing a single character in a C program, must I use "%1s" in the format string? Can I use something like "%c"? --------------Solutions------------- yes, %c will print a single char: printf("%c", 'h'); also, putchar/putc will work too. From "m

  • How to extract a single character (as a string) from a larger string in Ruby? 2008-12-16

    What is the Ruby idiomatic way for retrieving a single character from a string as a one-character string? There is the str[n] method of course, but (as of Ruby 1.8) it returns a character code as a fixnum, not a string. How do you get to a single-cha

  • String.lastIndexOf() is there a difference between a char and a string with a single character? 2009-01-22

    Just wondering if anyone knew off the top of their heads if there was much difference in doing the following: String wibble = "<blah> blah blah </blah>.... <wibble> blah wibble blah </wibble> some more test here"; int i = wibb

  • How can I check if a single character appears in a string? 2009-02-03

    In Java is there a way to check the condition: "Does this single character appear at all in string x" without using a loop? --------------Solutions------------- You can use String.indexOf(). String.contains() which checks if the string contains a spe

  • Python read a single character from the user 2009-02-04

    Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned. Sort of like getch(). I know that there is a function in windows for it, but I'd like something that is cross-pla

  • c++ outputting and inputting a single character 2009-02-20

    I am writing a program in c++ which implements a doubly-linked list that holds a single character in each node. I am inserting characters via the append function: doubly_linked_list adam; adam.append('a'); This function is implemented as follows: //A

  • Easy way to set a single character of an NSString to uppercase 2009-05-19

    I would like to change the first character of an NSString to uppercase. Unfortunately, - (NSString *)capitalizedString converts the first letter of every word to uppercase. Is there an easy way to convert just a single character to uppercase? I'm cur

  • Is there any benefit to using char instead of string for single-character values? 2009-06-09

    For .NET fields and properties that by definition only contain a single character, is there any benefit to defining them as char instead of string? Or is that an incorrect use of the char data type? I'm thinking of a field that can hold an M or F for

  • Why doesn't my string equality test for a single character work? 2009-06-24

    How does one compare single character strings in Perl? Right now, I'm tryin to use "eq": print "Word: " . $_[0] . "\n"; print "N for noun, V for verb, and any other key if the word falls into neither category.\n"; $category = <STDIN>; print "ca

  • Why is TextMate treating two spaces as a single character? 2009-07-10

    When I enter two spaces into TextMate 1.5.7, under certain circumstances it seems to treat the two spaces as a single character.. For example when typing ("[space][space]") (" |") \_ the cursor If I then press backspace, both spaces get deleted, and

  • Single character input with wake state 2009-07-15

    Back in college I wrote a game where the computer would sleep for 1 second, wake up and check to see if anything needed to be processed. Of course, if the user entered a single character command, it would respond immediately. Is there a way to do tha

  • regex to match a single character that is anything but a space 2009-07-25

    I need to match a single character that is anything but a space but I don't know how to do that with regex. --------------Solutions------------- The following should suffice: [^ ] If you want to expand that to anything but white-space (line breaks, t

  • check if a single character is a whitespace? 2009-09-30

    What is the best way to check if a single character is a whitespace? I know how to check this through a regular expression. But I am not sure if this is the best way if I only have a single character. Isn't there a better way (concerning performance)

  • How do I read a single character from STDIN using Perl on Windows? 2010-01-07

    Using Perl, how do I capture a single character from STDIN without needing the user to hit enter (similar to C's getch() function)? Perl has a getc() function, but according to the perlfunc: However, it cannot be used by itself to fetch single charac

  • How to handle single character search terms in MS-SQL FreeText searching? 2010-02-23

    I am having a problem with a FreeText search, currently running on a SQL 2000 server. In a table of approximately 1.3 million rows which contain company names, I am attempting to use a FreeText query. However since SQL Server strips out special chara

  • Is there a way to force just a single character from the onscreen keyboard 2010-04-13

    I'd like to invoke the Android onscreen keyboard to just retrieve a single character. Is there a way to do this? I am working on a hangman application and need to take input one character at a time. The plan is to have a text area for each guessed le

  • PHP in_array() can't even match a single character. Strict is set to true 2010-05-01

    I've seen a million of these threads here already, and read through every single one. That, plus some serious Googling. UPDATE: I am rewriting this post to include complete code and explanation, so everyone understands what is going on and what I am

  • What is the faster way of trying to find a single character on a String? 2010-05-27

    What of this code is faster/more efficient? : Boolean contains = myString.IndexOf("~", StringComparision.InvariantCultureIgnoreCase)!=-1; or Boolean contains = myString.IndexOf('~')!=-1; I think the second because is a single character, but using the

  • How to read a single character at a time from a file in Python? 2010-06-07

    Can anyone tell me how can I do this? --------------Solutions------------- with open(filename) as f: while True: c = if not c: print "End of file" break print "Read a character:", c Python itself can help you with this, in interactive mode:

  • How can i replace many special character including white space with single character? 2010-07-06

    How can i replace many special character including white space with single character? --------------Solutions------------- Unless it is a complex replace operation you might be fine using the Replace function: text = text.Replace("many special charac

  • Single character console input in java/clojure 2010-07-11

    How can I read a single character/key from the console without having to hit Enter? There is an old entry in Sun's bug database claiming that it can't be done in pure java. I've found these approaches JNI JLine [] Javacur

  • How do i read single character input from keyboard using nasm (assembly) under ubuntu? 2010-07-22

    I'm using nasm under ubuntu. By the way i need to get single input character from user's keyboard (like when a program ask you for y/n ?) so as key pressed and without pressing enter i need to read the entered character. I googled it a lot but all wh

Copyright (C), All Rights Reserved.

processed in 45.988 (s). 14 q(s)