-
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
-
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?
-
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
-
davinci, thanks for your reply, i was hoping for more like an example, but thanks anyway... :)
-
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.
-
davinci, thanks again i'll try to translate your code to C, becuase i don't know C++ thanks...
-
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.
-
kyrios,
I hav tested that program using gcc compiler
and What is the part u cant unterstand.
maybe a google search can help u.
-
Hi kyrios,
You can replace the last line of davinci's code with this line:
Code:
printf("TIME IS %d:%d:%d\n", tp->tm_hour, tp->tm_min, tp->tm_sec);
and don't forget to #include <time.h>
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,
<jdenny>
-
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?*/