DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    May 2005
    Posts
    22

    C Problem (Reading in input from user with loops)

    Hi this is just a simple question for programing in C.


    I have my main function using a while loop that gets 1 character at a time from the user.

    When the user enter '1' it calls a different method add().

    add() also asks the user for some input and does a simliair loop asking scanning in 1 character at a time.. The problem is that when I try inputing something in now it jumps back to the main loop.

    For Example:

    Please enter choice from 1-6:
    (I enter 1)
    It calls add()
    Please enter number:
    (I enter 54)
    It goes back to the main method without doing anything and gives me:
    Error: Please only enter number from 1-6.


    How do i make it so it stays in the add method until i actually return?

    Thx.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    You need to show us your loop.
    In short, you add() function has to have a similar loop that reads and validated input.
    Danny Kalev

  3. #3
    Join Date
    May 2005
    Posts
    22
    I sorta fixed the inital problem yet i still have one.
    This is the code. If you enter 1 from the initial menu it will go to the add method and ask for input..which works somewhat.

    The only problem i have now is that it always gets invalid input.

    Whats supposed to happen is you enter a Nameofitem then a space then a price.

    Like: Banana 1.34. it reads each thing 1 by one..it gets the right value and all however at the end it still says invalid.

    I thinks its because its forsomereson getting the last char to be " " or something. Im sorta stuck trying to fix this so if you got any ideas please help :D. Here is the code:


    #include <stdio.h>
    double total=0;
    int items=0;
    void printMenu();
    void addItem();
    int checkInput(char);
    int main(void){
    char c;
    printMenu();
    scanf("%c",&c);
    while(c!=EOF){

    if(c=='1'){
    addItem();
    }
    else if(c=='2'){
    printf("2\n");
    }
    else if(c=='3'){
    printf("3\n");
    }
    else if(c=='4'){
    }
    else if(c=='5'){
    total=0;
    items=0;
    printf("Cash register has been reset\n");
    }
    else if(c=='6'){
    printf("Quitting, have a nice day\n");
    exit(-1);
    }
    else{
    printf("Please only eneter a choice from 1 to 6\n");
    }
    printMenu();
    scanf("%c",&c);
    scanf("%c",&c);
    }
    return(0);
    }
    void printMenu(){
    printf("1. Add Item\n2. ComputeTotal\n3. Compute Average\n4. Compute Change\n5. Reset Cash-register\n6. Quit\nEnter Option:\n");
    return;
    }
    void addItem(){
    char x='n';
    double price=0.0;
    int findNum=0;
    int decimals=0;
    int check=0;
    printf("Please enter an item to add\n");
    while(x!=EOF){
    scanf("%c",&x);
    if(x==' '){
    findNum=1;
    continue;
    }
    else if(x=='.'){
    decimals=1;
    continue;
    }
    else if(findNum){
    check = checkInput(x);
    if(check==1 && decimals==0){
    /*found an acutal number (before the decimal) so add it to the price*/
    price=(price*10)+(double)(x-'0');
    continue;
    }
    else if(check==1 && (decimals==1 || decimals==2)){
    /*found an actual number after decimal*/
    if(decimals==1){
    price=price+((double)(x-'0')/10);
    }
    if(decimals==2){
    price=price+((double)(x-'0')/100);

    }
    decimals++;
    continue;
    }
    else if(check==1 && decimals==3){
    printf("too many decimals in number..invalid input please try again\n");
    addItem();
    }
    else if(check==0){
    printf("%f",price);
    printf("INVALID INPUT, please try again\n");
    addItem();
    }
    }
    }
    total=total+price;
    items++;
    return;
    }
    int checkInput(char x){
    if((x>='0') &&(x<='9')){
    return(1);
    }
    else{
    return(0);
    }
    }

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    I think you need to change the input type to a whole string, not just a single char. The problem is that when you read a char and the user presses something like 1+Enter, then the remaining char stays in the input buffer. It's read when the next input statement takes place.
    Danny Kalev

  5. #5
    Join Date
    May 2005
    Posts
    22
    ya i know but we cant use arrays :D

  6. #6
    Join Date
    Feb 2006
    Posts
    8
    if you cant use arrays, you can flush the input stream using fflush function.
    common usage:
    fflush(stdin);

    try this. meanwhile i shall give u some more inputs after working on this prob.

    bye for now
    jwala05 :)

Similar Threads

  1. java problem
    By Transform in forum Java
    Replies: 0
    Last Post: 11-18-2005, 05:54 PM
  2. Dynamic User Control Problem
    By webchetan in forum ASP.NET
    Replies: 1
    Last Post: 11-16-2005, 05:01 AM
  3. Check if an application is waiting for user input
    By Gerardo Gomez in forum VB Classic
    Replies: 6
    Last Post: 06-26-2000, 11:34 AM
  4. Creating a user input search
    By oSSiE in forum ASP.NET
    Replies: 0
    Last Post: 05-01-2000, 06:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links