Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,537 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,198 people online right now. Registration is fast and FREE... Join Now!




program not formating right

 
Reply to this topicStart new topic

program not formating right, Program displaying wierd output

jstephens
post 18 Nov, 2005 - 07:39 PM
Post #1


D.I.C Head

**
Joined: 7 Nov, 2005
Posts: 190



Thanked 1 times
My Contributions


I am very new to c programming, I just go done in my book with Statements.
I have two questions. One is why is my program showing more decimal places than necessary. My last question is how would I continue this so if I had more transactions to put in I would be able to.

/********************************
* FileName: cbcalc.c *
* *
* Purpose: Caluclate checkbook *
* *
* Author: Josh Stephens *
* *
* Date Created: 10/18/2005 *
* *
* Revision: 1.0 *
********************************/

#include <stdio.h>

int main()
{
float starting_balance; //Balance Currently in checkbook
float trans_amount; //Amount Transaction Was For
float ending_balance; //Amount after Transaction takes place
char trans_type; //(D) for Deposit and (W) for Withdraw

// Module 1. Get information From Users

printf("Please enter your starting balance:\t$");
scanf("%f",&starting_balance);

printf("Please enter (D) for Deposit and (W) for withdraw:\t");
scanf("%s",&trans_type);

printf("Please enter your Amount for Transaction:\t$");
scanf("%f",&trans_amount);

// Module 2. Take input and make decision then calculate based on transaction type

if(trans_type == 'D')
{
ending_balance = starting_balance + trans_amount;
}
else
{
ending_balance = starting_balance - trans_amount;
}

// Module 3. Print information Back to user

printf("Your new balance is now: \t%f$",ending_balance);

return(0);
}


Any Constructive criticism is very much welcomed.

This post has been edited by jstephens: 18 Nov, 2005 - 07:41 PM
User is offlineProfile CardPM

Go to the top of the page


born2c0de
post 18 Nov, 2005 - 09:38 PM
Post #2


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Use this method to set the number of decimal places for output:
CODE

printf("%.2f",ABC);

Here ABC is a Float and output shows two decimal places.
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 19 Nov, 2005 - 12:58 AM
Post #3


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Just one thing before I say anything else. I've noticed you declared main as int (integer). There really isn't a need to do this unless it's going to be returning a value at some point. In other words you may just want to declare it like so.

CODE


void main() //NOTICE: The void declaration means this function doesn't  return anything.
{

}



Theres many, many ways you can make this program continue on but many of them require a bit more knowledge of the language to utilize. In order to make a program run indefinately, a specific number of times or until a condition is met you have to use... I encourage you to look these up and play around with them a bit. If you don't understand it right away - no biggie. The compiler isn't going to bite you smile.gif. There's a brief description of some types of loops.

Loops

Keep in mind the implementation of many loops requires a solid understanding of Basic C concepts. Some of the loops you may have seen and will eventually encounter are:

Do-while loops.
Basically these loops do something while a certain condition is met. These are relatively simple to understand and easy to implement.

For loops
These loop a specific set of instructions a variable number of times depending on the situation. Simpler said. They loop as many times as you tell them too! smile.gif

while loops
Very similar to do-while loops except they usually run a set of instructions while a specific argument is NOT true.

Recursion

Recursion is not a loop in the sense of the others but does, effectively create a loop when implemented. Basically it entails calling the function from within itself and thereby restarting the function from beginning.

Below I've given you a simple piece of code that utilizes recursion.
CODE


void main()
{

// CODE HERE


main(); // NOTICE: This calls the function from within itself. Thus, A loop
}



You must be careful when doing this becuase this will loop the program forever. That's right - FOREVER. Unless you put something into the program to tell it to stop. Which is relatively simple but may still need to be learned. Try it out and see how it works. Later on you'll learn how to manipulate loops to work for you.
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 19 Nov, 2005 - 07:29 AM
Post #4


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


NEVER declare main as void!

The standard is int main()

void main is a very bad habit that you should never get into.
Programs are made to return an integer on their exit, and void main will not do that.


You can recurse int main like this:

CODE


int main()
{
   //Code goes here
   //I would suggest a condition to stop it from recursing for ever
   //Even better would be not to use int main as a recursive function
   //while loop seems to be the best solution for your problem
   //It has a very simple sintax
   //while( condition is true )
   //{
   //    Do this
   //}
   // Once condition evaluates false, it ends


   return main();
}


This post has been edited by Mrafcho001: 19 Nov, 2005 - 07:30 AM
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 19 Nov, 2005 - 07:46 AM
Post #5


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Hrm, I was taught otherwise. But I'll default to Mrafcho001. Listen to him smile.gif
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 19 Nov, 2005 - 07:48 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


QUOTE(Vextor @ 19 Nov, 2005 - 04:55 AM)
Just one thing before I say anything else. I've noticed you declared main as int (integer). There really isn't a need to do this unless it's going to be returning a value at some point.

It is returning a value...the last line is a return 0;

Generally speaking, it is a good habit to structure your main functions to return an integer, as it allows the program to 'speak' to another calling process.

Of course, it is not necessary if the the program will never be called by another.

The void vs int debate has raged for many a year...there are some good articles on the net about it.
User is offlineProfile CardPM

Go to the top of the page

jstephens
post 19 Nov, 2005 - 11:57 AM
Post #7


D.I.C Head

**
Joined: 7 Nov, 2005
Posts: 190



Thanked 1 times
My Contributions


Thanks Guys for all the help. It is now printing out the right amount.

I was getting things like 24.0400001. So after I learn about loops then I wil be able to rewrite this program to be able to run several transactions?

I agree with you Amadeus. The book I have now uses int main() while my Jasma's C\C++\C# book uses the void main(). When I start to write gui programs will either of these two cause problems.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 19 Nov, 2005 - 12:03 PM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


To be honest, it will depend upon your compiler, but in general, I would say no. When you are doing GUI development, however, you may find it more advatageous to use the int format...GUI programming involves a lot of processes calling other processes, to it's handy to have a method of passing the end status from one to the next.

I use int main myself, I find it to be useful and it complies to the ANSI standards.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 01:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month