Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Vibhor16

  • 2 years ago

In C if a user enters an input like "andjdnd" in a in a place holder of char then the program crashes.How can we prevent the crash and warn the user about his/her wrong input?

  • This Question is Closed
  1. DoodleTech
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Show your code. You said place hold of Char. When using C and inputting a string, you have to have an array place hold of the char type. and in the last place of the array you have to have \0, which signals the end.

  2. Vibhor16
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    This question came to me when i was answering a question.See the code here: http://openstudy.com/study?F11400362549395HFVVQ=_#/updates/4f0c037ee4b014c09e652046 Also how to check that the input is a char or an int?

  3. ecdown
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 4

    The issue is that you are using scanf with a %c argument, use a %s instead, this will just capture the first character typed in the input. Without that change what happens is that it takes the first character for the scanf with %c(which means only a single char) and the next set of chars for the next scanf and the return ends up blanking the final input. So the time = a the yrs = 0 (because of invalid input) and sal = 0.0000 One of the problems with validating the input with the code you are using is that there is a basic input validation happening. To insure that you can validate the input you need to capture it, then you can validate and convert as necessary. When you type a char that is not an int to a %d value, what you end up capturing it 0. Then there is no way to to see what was actually typed.

  4. Vibhor16
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    In other situations,assume : char name[10]; If a user types more characters(or some invalid character like #,$,%) then what piece of code should I include in my program to check ?There is a inbuilt function to check that which I can't recall.And if you can suggest any other solution?

  5. ecdown
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 4

    The following code is an an example of testing if an input string is a number or not: #include <stdio.h> int main() { char name[10]; int i=0; int isanumber = 1; scanf("%s",&name); while(name[i] != '\0' && i < 10) { if (name[i] >= '0' && name[i] <= '9') { } else { isanumber =0; } i++; } if (isanumber == 1) { printf("Is a number\n"); } else { printf("Not a number\n"); } return 0; }

  6. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.