GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Oct-2004, 18:48
agentxx04 agentxx04 is offline
New Member
 
Join Date: Sep 2004
Posts: 12
agentxx04 is on a distinguished road

Minor Problem with my program. Any help greatly appreciated.


Hi. I'm trying to make a program where the program asks for:
1. customer number
2. 1st letter of customer's last name
3. Amount

This is what I have so far:

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main() {

	int num;
	char lastname[1];
	float tempnum;
	float purchase[100];


		printf("Customer number:");
		scanf("%d", &num);
		printf("1st letter: ");
		getche();
		lastname[1] = getche();
		printf("\n");
		printf("Amount of purchase:");
		scanf("%f", &tempnum);
		purchase[100] = tempnum;
    printf("\n");


	return 0;

}

The only problem I have is that I want this program to keep asking the user those 3 questions until the user enters zero for the customer number. Like this:

Screen:
customer number?: 12
1st letter of customer's last name?: W
Amount?: 99.95

Customer number?:0


If anyone could give me some advice as to what I can do, I would greatly appreciate it.

Thanks!
:-D

P.S. - Does anybody know how to print out ammount in dollars and cents? ($1.50) ??:
  #2  
Old 23-Oct-2004, 20:58
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Use a do while loop whith choice >0

CPP / C++ / C Code:
do
{

3 questions code

} while (num>0);

Also if you just want to print dollar sign then use "$" sign in your printf statment.For e.g.

CPP / C++ / C Code:
printf("Amount : $%d\n",amount);
  #3  
Old 23-Oct-2004, 23:19
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by nkhambal
Use a do while loop whith choice >0

CPP / C++ / C Code:
do
{

3 questions code

} while (num>0);

Also if you just want to print dollar sign then use "$" sign in your printf statment.For e.g.

CPP / C++ / C Code:
printf("Amount : $%d\n",amount);

While your first suggetion works, your second is somewhat off. Isn't amount a floating point number? How does %d output a floating point value with 2 decimals?

A correct answer is %.2f for your format string. This prints the number with 2 decimals.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #4  
Old 24-Oct-2004, 05:21
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
Quote:
Originally Posted by WaltP
While your first suggetion works, your second is somewhat off. Isn't amount a floating point number? How does %d output a floating point value with 2 decimals?

A correct answer is %.2f for your format string. This prints the number with 2 decimals.
sorry...
i do not know to ask my question in here is right or not but it is related to this situation if it is wrong pls warn me!!!
in c we use %.2f but i want to implement in c++ in cout what is its format???
  #5  
Old 24-Oct-2004, 14:46
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi Waltp,

It was a general example and not as per the code in first post. :-)
  #6  
Old 24-Oct-2004, 15:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by nkhambal
Hi Waltp,

It was a general example and not as per the code in first post. :-)
As a seasoned programmer, I found your use of %d a confusing response for floating output since you did not mention it was "a general example" not a specific answer. I therefore guessed that if I was confused, what would a newbie think? They would use the code and wonder why it still didn't work.

I have no probelm with "general" answers (I perfer them myself) but make sure you write the response with no question that it's an idea of what to do, not an example of what will work. Avoids confusion...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 24-Oct-2004, 16:04
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Aye Aye.. :-)
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06
error during program rjd72285 C++ Forum 0 11-Nov-2003 19:49

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.