473,382 Members | 1,646 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,382 software developers and data experts.

Formatting C output

ShawnRR
Hi all,

This program is an easy "printing" Calculator... the problem i am having is that when I type in "0e" or "0E" for the program to exit it thinks i am typing in an exponent value and does not exit unless i type in "0ee" or "0EE". My question is how do i change this code around so when i type in "0e" or "0E" it exits the program.

Here is the code:
Expand|Select|Wrap|Line Numbers
  1. // Program 6-4 Simple printing calculator NOTE: Program won't accept "e" or "E" preceded
  2. // by a number because it thinks "e" stands for power of. Ex 10e12
  3.  
  4. #include <stdio.h>
  5. #pragma warning (disable: 4996)
  6.  
  7. int main (void)
  8. {
  9.     float accumulator = 0.0f, number = 0.0f;
  10.     char oper = ' ';
  11.  
  12.     printf ("Begin Calculations\n");
  13.  
  14.     do{
  15.         scanf ("%f %c", &number, &oper);
  16.  
  17.         switch (oper)
  18.         {
  19.             case 'S':
  20.             case 's':
  21.                 accumulator = number;
  22.                 printf("%f    content of accumulator\n", accumulator);
  23.                 break;
  24.  
  25.             case '/':
  26.                 if ( number == 0)
  27.                     printf ("Dividing by 0!\n");
  28.                                         break;
  29.                 else
  30.                     printf("%f    content of accumulator\n", accumulator = accumulator / number);
  31.                 break;
  32.  
  33.             case '*': 
  34.                 printf("%f    content of accumulator\n", accumulator = accumulator * number);
  35.                 break;
  36.  
  37.             case '+':
  38.                 printf("%f    content of accumulator\n", accumulator = accumulator + number);
  39.                 break;
  40.  
  41.             case '-':
  42.                 printf("%f    content of accumulator\n", accumulator = accumulator - number);
  43.                 break;
  44.  
  45.             case 'e':
  46.             case 'E':
  47.                 break;
  48.  
  49.             default:
  50.                 printf ("Unkown operator!\n");
  51.                 break;
  52.         }
  53.     }
  54.     while ( oper != 'E' && oper != 'e' );
  55.  
  56.         printf ("End of calculations.\n");
  57.  
  58.     return 0;
  59.  
  60. }
  61.  
The current output looks like this

Expand|Select|Wrap|Line Numbers
  1. Begin Calculations
  2. 22s
  3. 22.000000       content of accumulator
  4. 5 -
  5. 17.000000       content of accumulator
  6. 22 e
  7. End of calculations.
  8. Press any key to continue . . .
  9.  
I would like to make it look like this:

Expand|Select|Wrap|Line Numbers
  1. Begin Calculations
  2. 22s                 Set Accumulator to 22 (Print this line on the same line as input)
  3. 22.000000       content of accumulator
  4. 5 -                  Subtract 5
  5. 17.000000       content of accumulator
  6. 22 e               End of program
  7. End of calculations.
  8. Press any key to continue . . .
  9.  
Basically what I am asking is how do I make the program print a line of text on the same line of the user input line or the scanf line.
Sep 13 '07 #1
9 2208
Savage
1,764 Expert 1GB
::snipped quote so post will show::


Wait a second what is your problem:

1.) To get 0e to exit the program or
2.) To print the text on the same line where user inputs data?

Savage
Sep 13 '07 #2
sicarie
4,677 Expert Mod 4TB
Basically what I am asking is how do I make the program print a line of text on the same line of the user input line or the scanf line.
Don't using newline characters ('\n') in your output strings where you want to have something after it.
Sep 13 '07 #3
::snipped quote so post will show::


Wait a second what is your problem:

1.) To get 0e to exit the program or
2.) To print the text on the same line where user inputs data?

Savage
Savage,

My problem is both. I want the program to exit when i type 0e and also i want to be able to output with printf on the same line where i input with scanf.

thanks
shawn
Sep 13 '07 #4
Savage
1,764 Expert 1GB
Savage,

My problem is both. I want the program to exit when i type 0e and also i want to be able to output with printf on the same line where i input with scanf.

thanks
shawn
If you want it to exit when e is typed,you will need to store number in a char array,search for the occurrence of e and if any then exit.Second problem has been answered by sicarie.

Savage
Sep 13 '07 #5
RRick
463 Expert 256MB
You're problem lies with scanf and reading %f, i.e. a float. Since floats can take an exponential, scanf is going to consume any 0e you send it and convert it into a float.

Why not make the "break" command a q? Scanf will never read a 'q' into a float.
Sep 14 '07 #6
If you want it to exit when e is typed,you will need to store number in a char array,search for the occurrence of e and if any then exit.Second problem has been answered by sicarie.

Savage
Savage the only problem with Sicarie's answer is that scanf automatically seems to continue to the next line when user inputs his value. I know how to make the printf print on the same line before the user input but i want to printf on the same line after the user input. Also on storing number in a char array can you give me a sample code on how i would than do the arithmetic operation using that char array since the user inputed numbers are stored as symbols and not as actual numbers.

Thanks
Sep 14 '07 #7
You're problem lies with scanf and reading %f, i.e. a float. Since floats can take an exponential, scanf is going to consume any 0e you send it and convert it into a float.

Why not make the "break" command a q? Scanf will never read a 'q' into a float.
RRick,

I totally agree with you except that the programming exercise specifically asks to use E or e as the break point.
Sep 14 '07 #8
Savage
1,764 Expert 1GB
Savage the only problem with Sicarie's answer is that scanf automatically seems to continue to the next line when user inputs his value. I know how to make the printf print on the same line before the user input but i want to printf on the same line after the user input. Also on storing number in a char array can you give me a sample code on how i would than do the arithmetic operation using that char array since the user inputed numbers are stored as symbols and not as actual numbers.

Thanks
Use wherey and wherex functions to remember current coordinated,and then just after scanf reduce y for one and use gotoxy(x,y) to return to the previos line.

About your second question,you can use atoi to convert form char[] to int,enabling you to do anything with it.

Savage
Sep 14 '07 #9
RRick
463 Expert 256MB
Shawn,

You're going to have to get rid of the %f in your scanf or else put a space between the number and command in your input.

If you can't put in a space for the command, then read everything into a char buffer and extract the command before you parse for the float.
Sep 14 '07 #10

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

Similar topics

6
by: Tom Petersen | last post by:
Here is a little more info, sorry should have explained what my final goal was. I am creating a .vcs file from a form to import into Outlook. I was just testing the output on screen then pasting...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
2
by: Ken Wilson | last post by:
I am writing and .xml file and am not getting the formatting I would like. The portion of the code that is giving me problems is as follows; XmlTextWriter tw = new XmlTextWriter(filename); ...
8
by: Vinay Jain | last post by:
Hi.. I am newbe in postgresql so please help me though the question may be very easy to answer.. Is there any formatting function to get output with fix lengths..for example my query is.. schema...
9
by: sck10 | last post by:
Hello, I have a page with an ImageButton that is used to redirect to another page. When the page first opens, everything looks as expected. However, when I click on the image, the new page...
6
by: Rafael Olarte | last post by:
The goal of this project is to output the following information as follows: 34.5 38.6 4.1 42.4 3.8 close 46.8 4.4 big change. The values of the first colunm are obtain from a file...
1
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.