Thread: Hi~~i just using struct in C for practicing my tutorial question..

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    16

    Hi~~i just using struct in C for practicing my tutorial question..

    Code:
    #include<stdio.h>
    #include<string.h>
    struct infor{
    	char name[20];
    	int age;
    	char sex[6];
    };
    struct center{
    	char *comname;
    	char *address;
    	char *phone;
    };
    float get_kilogram(float KiloGram)
    {
    	float Cost;
    	if(KiloGram < 3.00)
    		Cost = KiloGram * 1.20;
    	
    	else if(KiloGram >= 3.00 && KiloGram <= 6.00)
    		Cost = KiloGram * 1.00;
    	
    	else 
    		Cost = KiloGram * 0.80;
    	
    	return Cost;
    }
    int main (void)
    {
    	struct infor INFO;
    	struct center ctr;
    	float Kgs,Price;
    	
    	ctr.comname = "UNIVERSITY TUNKU ABDUL RAHMAN";
    	ctr.address = "Universiti Tunku Abdul Rahman,\n\tP.O.BOX 11384, 50744 Kuala Lumpur, Malaysia";
    	ctr.phone = "Tel: (+6)03-79582628";
    	
    	printf("Please Enter Customer's Name:\n");
    	scanf("%s", &INFO.name);
    	printf("Please Enter Customer's Age:\n");
    	scanf("%d", &INFO.age);
    	printf("Please Enter Customer's Sex:\n");
    	scanf("%s", &INFO.sex);
    	printf("Please Enter Customer's Laundry Weight (In KG(s))\n");
    	scanf("%f", &Kgs);
    
    	Price = get_kilogram(Kgs);
    	
    	printf("\t\t%s", ctr.comname);
    	printf("\n");
        printf("\t\t%s", ctr.address);
    	printf("\n");
    	printf("\t\t%s", ctr.phone);
    	printf("\n");
    	printf("Customer's Name : %s\n", INFO.name);
    	printf("Customer's Age : %d\n", INFO.age);
    	printf("Customer's Sex : %s\n", INFO.sex);
    	printf("Price Per Kilogram(s) : \n");:confused:
    	printf("Payment : RM%.2f\n", Price);
    	
    	return 0;
    }
    The underline one is need to printout the price per KGs..
    but i cant called it out from the if else function prototype...
    could someone advice me??

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "can't call it from the if else"? Do you not know the syntax, or is there some restriction on "thou shalt not, for this assignment, call the function from the if else"?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this...
    Code:
    	printf("Price Per Kilogram(s) : %.2f\n", Price);
    You will also want to print a total...
    Code:
        printf("Total for this load : %.2f\n", Price * Kgs);
    For more information, look up printf() in your C library documentation.

    Although I'm a little confused about why you need the customer's name, age and sex to calculate the price of a load of laundry...
    Last edited by CommonTater; 08-02-2011 at 10:11 AM.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    Like this...
    Code:
    	printf("Price Per Kilogram(s) : %.2f\n", Price);
    You will also want to print a total...
    Code:
        printf("Total for this load : %.2f\n", Price * Kgs);
    For more information, look up printf() in your C library documentation.

    Although I'm a little confused about why you need the customer's name, age and sex to calculate the price of a load of laundry...
    i just following the question need.
    the question show me it will read customer name, age, sex and laundry weight and print out a official receipt..

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... that's cool... At least there's a reason for it. I favour a minimalist approach... never ask questions you don't need to ask...

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    Ok... that's cool... At least there's a reason for it. I favour a minimalist approach... never ask questions you don't need to ask...
    erm~
    usually how can make a loop that ask a user whether wants to continue or not?? (Y/N)... but i try it with do while loop.. but cant be done.. then if user key in 'n'.. then the program will terminate...

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Vinsento View Post
    erm~
    usually how can make a loop that ask a user whether wants to continue or not?? (Y/N)... but i try it with do while loop.. but cant be done.. then if user key in 'n'.. then the program will terminate...
    Yes it can... in fact it's rather easy in your program... Ask yourself... If I get to the end, where to I have to go back to to start over? ... then put that section of code in a loop...

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    Ok... that's cool... At least there's a reason for it. I favour a minimalist approach... never ask questions you don't need to ask...
    erm~
    usually how can make a loop that ask a user whether wants to continue or not?? (Y/N)... but i try it with do while loop.. but cant be done.. then if user key in 'n'.. then the program will terminate...

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Is there an echo in here?

    Code:
    while condition is true
    {
      do stuff
      print "continue y/n"
      read input
      if input is 'N' or 'n', condition = false
    }

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Vinsento View Post
    usually how can make a loop that ask a user whether wants to continue or not?? (Y/N)... but i try it with do while loop.. but cant be done.. then if user key in 'n'.. then the program will terminate...
    It most certainly can be done with a do-while loop or a while-loop or a for-loop or any kind of logical loop you can think of. Why don't you actually start trying to learn how to program? We are not going to teach you the C language one concept at a time.

    CProgramming C Tutorials
    Teach yourself C in 21 Days
    Steve Summit's Intro C Tutorial
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    16
    Quote Originally Posted by Matticus View Post
    Is there an echo in here?

    Code:
    while condition is true
    {
      do stuff
      print "continue y/n"
      read input
      if input is 'N' or 'n', condition = false
    }
    the true & false need to declare first right?? izzit using bool file??

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Matticus showed pseudocode.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Matticus showed pseudocode.
    OP... In case you don't know what pseudocode is, it is a way of representing a function or series of instructions in a gramattically loose way so that we can better understand a sequence of events. It's not program code and it ain't going to compile.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    16
    Quote Originally Posted by CommonTater View Post
    OP... In case you don't know what pseudocode is, it is a way of representing a function or series of instructions in a gramattically loose way so that we can better understand a sequence of events. It's not program code and it ain't going to compile.
    then what code should i used??

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Vinsento View Post
    then what code should i used??
    C code you wrote --by yourself-- based on the pseudocode presented.

    Message of the day: We're here to help you with problems in YOUR code, we are not here to write code for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct delcaring a struct within itself question
    By illidari in forum C Programming
    Replies: 3
    Last Post: 12-03-2010, 03:01 PM
  2. Google practicing Voodoo in Europe
    By MK27 in forum General Discussions
    Replies: 18
    Last Post: 07-14-2010, 11:09 PM
  3. Still Practicing, Too Much To Learn
    By dolfaniss in forum C Programming
    Replies: 6
    Last Post: 05-21-2010, 01:27 PM
  4. Question about list (struct {..struct *next}
    By jmzl666 in forum C Programming
    Replies: 1
    Last Post: 10-23-2002, 01:34 AM
  5. New to C++, tutorial question
    By UniqueScreenNam in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2002, 09:56 PM