New at C

This is a discussion on New at C within the C Programming forums, part of the General Programming Boards category; I am new at C-been studing it for 2 weeks, and really confused. I thought if I joined a forum ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Angry New at C

    I am new at C-been studing it for 2 weeks, and really confused. I thought if I joined a forum you guys who are more experienced could explain some of the concepts better than a teacher because maybe My problem is writing a C programming that displays some taxes. 3 different towns have different sales tax but the same purchase amount.
    Berlin=7.25%
    Marlo=7.5%
    Teymon=7.75%
    The purchase amount=125

    I started this way:

    #include <stdio.h>
    main()
    {
    char 1 = Berlin
    char 2 = Marlo
    char 3 = Teymon

    a = .0725
    b = .075
    c = .0775
    pur_amt = 125

    I know it's simple, but to me it's like a foreign language. I guess I sort of get lost from there. Please offer any suggestions or let me know if I'm on the wrong/right track. Please explain, no just give me answe because I want to learn from the example you give me. I am using Miracle C-is that ok?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by bigsmiles4ya
    I am using Miracle C-is that ok?
    No. Try Dev-C++.

    Variables need to be declared of a specific type.
    Code:
    double Berlin = .0725;
    double Marlo  = .075;
    double Teymon = .0775;
    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.*

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469

    custom written tutorial just for you

    i have written the following to explain to you some of the fundamental mistakes in your code, where they are wrong, and how you can learn from them and fix it. enjoy


    lots wrong with that, but its easily fixed.


    firstly, you need to learn about data types: the computer has different "keywords" which tell it how much space the specific data needs. you declared variables of type char, which is a data type pretty much specifically for characters, but you need to swave numbers. there is more than one data type designed to represent numbers: int, float and double. (to name the big 3)

    let me firstly say that it should (hopefully) be perceptually obvious that to store a number like 2 in binary (as the computer does) should require much less space than storing a number like .00005157456575 (just for a ludicrously long example).

    but that number is smaller than 2??? well it is, but its also much more precise than just 2 - therefore it will take more 1's and 0's in the computer to represent it.

    without getting into memory specifics:
    int is a data type designed to hold integers (i.e positive or negative whole numbers)
    float is designed to store more detailed numbers, such as numbers accurate to a few decimal places
    double is like float only there is more space allocated - more even more detailed (more decimal place) numbers.

    the first thing you need to do is declare variables to hold all your numbers and names - there are a few rules. firstly, all your declarations should be at the very start of the program. secondly, variable names cant begin with numbers or symbols, but that doesnt mean they cant contain numbers and some symbols afterwards. thirdly, choose the type carefully.

    declaring variables is the way that you tell the compiler to make some space in memory for a variable. the way it goes is to state the name of the data type, (int, char...), then give it a name. the name you assign it is how you will refer to that data later in your program. you had char 1 = Berlin and so on. as stated, the number must go, but another problem is that a char data type can only hold a single character - Berlin is 6 so that wont work.

    later on you have a=0.725 and so on - this is illegal because when you try to compile, the computer will think that "a" is a variable, but you havent declared it in the manner described above for char, and thats a problem.

    as you might be able to see by now, there is a lot you have to learn, so i'll point you in the right direction:

    My problem is writing a C programming that displays some taxes
    here's how you would declare the variables to store your tax rates:

    Code:
    float Berlin = 0.0725;
    float Marlo = 0.075;
    float Teymon = 0.0775;
    
    int  pur_amt = 125;
    notice the variable names are related to the tax rates? that is sensible programming: you didnt need to declare variables of char for the city names - you can represent your information in a format like that. also note the choice of data types, and for pur_amt i used int since you don't have any decimal places. i could just as easily used float - and if i was later doing calculations involving pur_amt and one of the tax rates - i would have used float. that is a topic which i would recommend you find out about in the tutorial section of this site, i'm only writting this as a detailed description of your problem.

    finally, you are ready to write to the screen - since you said that you want to learn by example, i have written this full program

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	float Berlin = 0.0725;
    	float Marlo = 0.075;
    	float Teymon = 0.0775;
    	int  pur_amt = 125;
    
    	printf ("The tax rate in Berlin is %f\n", Berlin);
    	printf ("The tax rate in Marlo is %f\n", Marlo);
    	printf ("The tax rate in Teymon is %f\n\n", Teymon);
    	printf ("The purchase price is the same in all: %d\n", pur_amt);
    	
    	return 0;
    }
    this should really confuse you now! i won't explain it, but i will refer you to the tutorial section of this site - they have really great articles explaining very carefully from the basic which i outlined here. needless to say, once you understand what i wrote, you are not far away from writing real programs for yourself, just like the one above.

    finally, as regards Miracle C, it was my first home compiler and i found it good to start out, and when you have learned more, you can graduate to a more detailed and powerful compiler which allows you to write more complex programs. miracle could suit you for up to a year or more, unless you're an avid programmer. then eventually you will need a change to something more reliable.

    i hope this helps, and i'm sorry if you didn't want an explanation this detailed, but the truth is, you need to get the basics of declaring variables described to you. i hope this was clear enough to follow, here's a link to the tutorial section of the site

    http://www.cprogramming.com/tutorial.html
    Last edited by Richie T; 01-24-2006 at 08:41 AM. Reason: i probably overestimated the life of Miracle
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    I love you guys! I have been reading all of these books and nothing has made sense until you made that post. Maybe you should write your own book for dummies. Thanks so much, and I'm sure you'll be hearing from me again. Oh, I forgot...I realyy appreciate you not being harsh and thinking I was a total idiot. Some people are so mean and think that we were born knowing this stuff, when none of are born knowing anything. we have to learn it all. Thanks again, I feel like a owe you something.

Popular pages Recent additions subscribe to a feed

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