a simple question about do while statement and choice options

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyRedz
    New Member
    • Jan 2007
    • 17

    a simple question about do while statement and choice options

    i want to do a program that requires user to input float numbers,charact ers like this
    Code:
    printf("please enter the value with its SI unit...");
    scanf(" %lf %c",&value,&si);
    but then there is an escape statement thats says or q to quit ..
    so i assume it needs units or character to quit...
    but then how to integrate it with my normal asked input like the first..



    i assume this but dunno of its compilabilty

    Code:
    
    #include<stdio.h>
    
    char choice;
    float value;
    int main()
    
    {
    
    do{
    printf("this is a program by ME\n);
    printf("please input values followed with its SI unit or q to quit..\n);
    scanf(" %lf %c"&value,&choice);
    }while(choice!=q)
    printf("have a nice day\n");
    
    return 0;
    i don't get is the q to quit..how can it be read only by itself because there is a float value with it???
    any help???
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Lexical analisys of manual user input is quite complicated. Better read an entire
    line first and process it later; if the sscanf(buffer, "%f %c, &f, &c) function call
    doesn't return 2 it couldn't parse a float number and a character; next you could
    try to process the line as sscanf(buffer, " %c", &c) and check whether or not c
    contains the value 'q'. Don't forget to sprinkle in ample prompts because those
    users never seem to understand what they've been asked to do.

    kind regards,

    Jos

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      You need to scanf it using "%s" specifier ( ok I know it is prone to buffer overflow ) like
      Code:
       char buffer[100];
      scanf("%s", buffer );
      Then check it if it is equal to "q" string using strcmp, if yes then quit, else try to convert this string to float using sscanf.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        The 'q' is not in with the float it is separated from the float by a comma.
        All that is apparently needed is some code like:
        Code:
        if(choise='q') return:

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by whodgson
          The 'q' is not in with the float it is separated from the float by a comma.
          All that is apparently needed is some code like:
          Code:
          if(choise='q') return:
          = != ==

          Comment

          Working...