473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

a simple question about do while statement and choice options

17
i want to do a program that requires user to input float numbers,characters like this
Expand|Select|Wrap|Line Numbers
  1. printf("please enter the value with its SI unit...");
  2. scanf(" %lf %c",&value,&si);
  3.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include<stdio.h>
  4.  
  5. char choice;
  6. float value;
  7. int main()
  8.  
  9. {
  10.  
  11. do{
  12. printf("this is a program by ME\n);
  13. printf("please input values followed with its SI unit or q to quit..\n);
  14. scanf(" %lf %c"&value,&choice);
  15. }while(choice!=q)
  16. printf("have a nice day\n");
  17.  
  18. return 0;
  19.  
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???
Oct 17 '08 #1
4 2247
JosAH
11,448 Expert 8TB
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
Oct 17 '08 #2
newb16
687 512MB
You need to scanf it using "%s" specifier ( ok I know it is prone to buffer overflow ) like
Expand|Select|Wrap|Line Numbers
  1.  char buffer[100];
  2. scanf("%s", buffer );
  3.  
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.
Oct 17 '08 #3
whodgson
542 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. if(choise='q') return:
Oct 21 '08 #4
r035198x
13,262 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. if(choise='q') return:
= != ==
Oct 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
17
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
22
by: tricard | last post by:
Good evening, I was thinking about making a very simple DOS based (console) menu for a project that i am working on, but I have no idea where to start for something like this. Does anyone have...
6
by: Shawn Minisall | last post by:
I've been having some problems with using a while statement for one menu within another while statement for the main menu, first time I've done it. It's with choice number two from the menu. When...
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
7
by: CSharper | last post by:
Yesterday I had a heated discussion with my colleagues on what is a data centric application and having business logic in sql. I have group of people who wants to include all the business logic in...
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.