Can't understand what I'm doing wrong.

This is a discussion on Can't understand what I'm doing wrong. within the C Programming forums, part of the General Programming Boards category; Write a program which stores coded information about sales for customers. Each customer has a unique number from 1 to ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Unhappy Can't understand what I'm doing wrong.

    Write a program which stores coded information about sales for customers. Each customer has a unique number from 1 to 100. The program prompts the user for :

    1. the customer number
    2. the first letter of the customer's last name
    3. the amount of the customer's latest purchase.

    It stores the letter in one array and the purchase amount in another. There should be no more than two arrays in your program.

    The program will keep asking for this info until the user enters a customer number of zero.


    To print the sales amount in dollars and cents, use the $%.2f format specifier.

    On screen it should look like this:

    customer number? 12

    first letter of customerís last name? J

    amount of purchase? 57.62

    This is what I have so far:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main() {
    
    	int num;
    	char lastname[1];
    	float tempnum;
    	float purchase[100];
    
    
    	do {
    		printf("Customer number: \n");
    		scanf("%d", &num);
    		printf("1st letter: \n"
    		getche();
    		char lastname[1] = getche;
    		printf("Amount of purchase: \n");
    		scanf("$%.2f", &tempnum);
    		purchase[100] = tempnum;
    		} while(num!= '0');
    
    	return 0;
    
    }
    I thought I was doing this right, but when I tried to execute this program, it couldn't run beacuse there were errors. If anyone could look over what I have and offer some tips on what I' m doing wrong, I would greatly appreciate it.

    Thank You.
    Last edited by agentxx04; 10-21-2004 at 06:02 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    And what were the errors?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Helpful hints:
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Here are the errors.

    These were the errors:

    Error 16: Function call missing ) in function main()
    Error 17: Cannot convert 'int (*)()' to 'char[1]' in function main()
    Error 19: Function call missing ) in function main()

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Code:
    printf("1st letter: \n"
    Where is the closing parentheses?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    Oops!!! I can't belive I didn't see that!!!! I now fixed that & now there's one error left. Thank You for pointing that out.

    OK, here's my updated program.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main() {
    
    	int num;
    	char lastname[1];
    	float tempnum;
    	float purchase[100];
    
    
    	do {
    		printf("Customer number: \n");
    		scanf("%d", &num);
    		printf("1st letter: \n");
    		getche();
    		char lastname = getche();
    		printf("Amount of purchase: \n");
    		scanf("%.2f", &tempnum);
    		purchase[100] = tempnum;
    		} while(num!= '0');
    
    	return 0;
    
    }
    Last edited by agentxx04; 10-21-2004 at 10:50 PM.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    char lastname[1] = getche;
    getche needs to be getche(); it is function. Plus you really have to learn to read those errors and figure them out, because a simple syntax error should be noticed immediatly.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    Well, I fixed the getche(), & there's no errors now. I'm trying to make the program to appear on the screen like this:

    Customer Number? : 3
    1st letter: W
    Amount of purchase? :

    But instead this appears:

    Customer Number? 3
    1st letter:
    Amount of purchase? :

    I keep inputting a letter & the thing just vanishes.

    If anyone can figure out why this is happening, I would really appreciate it.

    Thanks!

    P.S. - Does anyone know how to print out values in dollars & cents?
    ex. $100.20

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    try using a standard function that is implemented almost the same way(except it returns an int) getchar()
    so
    first change
    Code:
    char lastname[1] = getche;
    to
    Code:
    lastname[0] = getchar();
    and aslo
    Code:
    char lastname[1];
    to
    Code:
    int lastname[1];

  10. #10
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I assume this is what you have now....
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main() {
    
    	int num;
    	char lastname[1];
    	float tempnum;
    	float purchase[100];
    
    
    	do {
    		printf("Customer number: \n");
    		scanf("%d", &num);
    		printf("1st letter: \n");
    		getche();
    		lastname[1] = getche();
    		printf("Amount of purchase: \n");
    		scanf("%f", &tempnum);
    		purchase[100] = tempnum;
    		} while(num!= '0');
    
    	return 0;
    
    }
    The problem is here....
    PHP Code:
            printf("1st letter: \n");
            
    getche();
            
    lastname[1] = getche();
            
    printf("Amount of purchase: \n"); 
    The getche() function takes one keystroke from the keyboard and echoes the key pressed. In this case first getche(); takes the keystroke W (from your example) and then lastname[1]=getche(); waits for another keystroke. Which I am assuming is when you press the Enter Key, which is another keystroke which is echoed. So it's echoing a carriage return, but not a newline (or is it vice versa? I get confused on the 2 terms sometimes) which basically pushes the cursor back to the beginning of the line but does not step down to a new line. The following printf("Amount of purchase: \n"); then prints from that point. Causing the illusion of printing over the W.


    Not only that, but the only thing stored in lastname[1] is the carriage return, not the letter you pressed. And it's beyond me why you're setting up a 1 character array. Just use a char type variable.

    A simple solution (other than considering a complete rewrite) would be to lose the first getche() function call, and add a \n to the beginning of the printf's conversion string.

    Consider using getchar() vs getche() (since getche isn't portable anyhoo).
    Last edited by Scribbler; 10-21-2004 at 11:17 PM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    My professor said to use the getche( ) function for inputting the first letter of the customer's last name. So, I can't get rid of it. It's a shame to cause when I replaced it with getchar(), the problem was fixed.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by linuxdude
    try using a standard function that is implemented almost the same way(except it returns an int) getchar()
    so
    first change
    Code:
    char lastname[1] = getche;
    to
    Code:
    lastname[0] = getchar();
    and aslo
    Code:
    char lastname[1];
    to
    Code:
    int lastname[1];
    Didn't we just go over this? Must have been someone else...

    Unless the value is EOF, it is perfectly fine to assign the return value of getch* to a char. So there is no need to make lastname an int.

    Although I do question the logic of using a one element array...

    However, what's being forgotten (that you've stated, that they're likely ignoring) is that array indexing starts at zero. So the line should be again, accessing element zero, and not one.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21