Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: C Loop question

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Posts
    149

    C Loop question

    Hi, I am currenlty updating a calculator that I made a while ago. Here is part of the code

    printf("\nWould you like to see the instructions (y) or (n): ");
    scanf(" %c", &answer); // gets users answer of yes or no
    while ((toupper(answer) != 'Y') || (toupper(answer) != 'N'))
    { printf("\nPLease enter Y or N: ");
    scanf(" %c", &answer); // Makes sure user enters a y or n
    }
    if (toupper(answer) == 'Y')
    {prinntf("Instructions")
    }
    if (toupper(answer) =='N')
    { printf("Fine! \n");
    }
    /*********************/
    then the program continues here...
    Is there a way for me too loop using 'while' to make sure the user inputs a number when asked for it?for example:

    printf("\nEnter calculation: ");
    scanf(" %f", &amount1); //gets the first digits
    scanf(" %c", &symbol); //gets calc symbol /*Must be in this order form*/
    scanf(" %f", &amount2); //gets the last digits
    if (symbol == '/')
    { total = amount1 / amount2; //divides 1st and 2nd amount
    printf("The total is %f. \n", total);
    }

    can i use something for example:
    while (amount1 != ANUMBER)
    {do this} // so the program can make sure the user inputs a number, and nothing else but a number...
    Becuase if they input anything else it comes upt with bad results... Can anyone help me ? thanks

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    And i forgot to add... How can i add the current time, like Month/Day/Year, and current time, in the code with C.??? Does anyone know how?

  3. #3
    Junior Member
    Join Date
    Feb 2005
    Posts
    24
    kyrios,

    I think time.h header file should contain important info for u.
    u can refer in ur man or look here:
    http://www.opengroup.org/onlinepubs/...sh/time.h.html

    do tell if it dosent help

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    davinci, thanks for your reply, i was hoping for more like an example, but thanks anyway...

  5. #5
    Junior Member
    Join Date
    Feb 2005
    Posts
    24
    hi

    I was unable to give example tht time here it is:

    Code:
    long t;
    struct tm *tp;
    time(&t);
    tp=localtime(&t);
    cout<<"TIME IS "<<tp->tm_hour<<":"<<tp->tm_min<<":"<<tp->tm_sec<<endl;
    Similarly u can add other stuff to
    hoping it helps u.

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    davinci, thanks again i'll try to translate your code to C, becuase i don't know C++ thanks...

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    I really could not understand your C++ code. I would appreciate if someone has an example of how time is used in a program. I would like to see the part of your code that has time print to the screen.

  8. #8
    Junior Member
    Join Date
    Feb 2005
    Posts
    24
    kyrios,

    I hav tested that program using gcc compiler
    and What is the part u cant unterstand.

    maybe a google search can help u.

  9. #9
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Hi kyrios,
    You can replace the last line of davinci's code with this line:
    Code:
    printf("TIME IS %d:%d:%d\n", tp-&gt;tm_hour, tp-&gt;tm_min, tp-&gt;tm_sec);
    and don't forget to #include &lt;time.h&gt;

    As for the other question, I suggest you take user's input from the arguments passed to the program.
    So the user would enter:
    calculate 57 * 3
    And then your program replies:
    171

    Peace always,
    &lt;jdenny&gt;
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  10. #10
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    Okay Jdenny, thanks a lot for the time code. So I guess for the variables it would be

    int tm_hour, tm_min, tm_sec; /*is that the way i would use the variables?*/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •