First C class, yay. Alternatives to DevC++?

BillLeeLee

[H]F Junkie
Joined
Jul 2, 2003
Messages
13,486
I'm actually amazed that compiles, especially when it contains this:

Code:
#define year     365
#define beer_calories         150
#define weight                15/365
int main()
{
    float rate, beer, total_beers, cost, total_cost, total_calories, total_weight;
    [b]year, beer_calories, weight;[/b]

You should be getting syntax errors at least for that. And why do you use SYSTEM PAUSE, because normally the executable spawns a CMD window that quickly exits?

Do you really need to #define your constants? I'd like to think that C has progressed to where you can use something 'safer', like consts
 

_cashel

[H]ard|Gawd
Joined
Jul 9, 2002
Messages
1,784
mikeblas said:
  • Should have a space between "include" and "<".
  • Abusing #define, and you're lucky it works right.
  • No error checking.
  • Doesn't specify units.
  • Spawns "PAUSE" on the system unconditionaly, even though it's only meant for debugging.
  • Doesn't check return code from system().
  • Should use internals instead of "PAUSE".
  • Kind of odd to begin output strings with newline instead of end 'me that way. You exit without a newline on your last output line as a result of this.


BillLeeLee said:
I'm actually amazed that compiles, especially when it contains this:

You should be getting syntax errors at least for that. And why do you use SYSTEM PAUSE, because normally the executable spawns a CMD window that quickly exits?

Do you really need to #define your constants? I'd like to think that C has progressed to where you can use something 'safer', like consts

:D, I figured I'd have a ton of errors. To be honest, I really don't have much of a clue of what I'm doing, I'm just kind of figuring it out in the dark, so I appreciate the help. I'm going to edit this post in a bit asking for help specifially on the things you guys just mentioned.
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
_cashel said:
:D, I figured I'd have a ton of errors. To be honest, I really don't have much of a clue of what I'm doing, I'm just kind of figuring it out in the dark, so I appreciate the help. I'm going to edit this post in a bit asking for help specifially on the things you guys just mentioned.
I found this(google cache, since site is down) very helpful in increasing my understanding of the const keyword.
 

_cashel

[H]ard|Gawd
Joined
Jul 9, 2002
Messages
1,784
Alright, so if I'm abusing #define, should I just take those constants and put the numbers in manually into the code? I thought I had done that earlier and it wasn't working right, but I'll try it again.

  • No error checking.
  • Doesn't specify units.
  • Spawns "PAUSE" on the system unconditionaly, even though it's only meant for debugging.
  • Doesn't check return code from system().
  • Should use internals instead of "PAUSE".

you've pretty much lost me on those. How do I enable error checking. By specifying units, you mean like how much beer, etc.? The rest I don't really know where to start. I put the PAUSE in there because it was closing the program out immediately, before you could see the results.

BillLeeLee said:
I'm actually amazed that compiles, especially when it contains this:

Code:
#define year     365
#define beer_calories         150
#define weight                15/365
int main()
{
    float rate, beer, total_beers, cost, total_cost, total_calories, total_weight;
    [b]year, beer_calories, weight;[/b]

You should be getting syntax errors at least for that. And why do you use SYSTEM PAUSE, because normally the executable spawns a CMD window that quickly exits?

Do you really need to #define your constants? I'd like to think that C has progressed to where you can use something 'safer', like consts

why would I be getting syntax errors there? It may be pretty obvious, but I'm obviously pretty new to this. I appreciate the help guys.
 

BillLeeLee

[H]F Junkie
Joined
Jul 2, 2003
Messages
13,486
drizzt81 said:
I found this(google cache, since site is down) very helpful in increasing my understanding of the const keyword.

const in C++ doesn't behave the same way that it does in C. I'm not sure if that site notes the differences (because even the cache link isn't working for me at the moment).

Here's a simple example:

C:
Code:
const int size = 100;
char buff[size];

This will not work!

C++:
Code:
const int size = 100;
char buff[size];

The above will work.

Here's one explanation on why: http://c-faq.com/ansi/constasconst.html

K and R also make a reference to const in C basically making the variable read-only. C++ const treats it like you would think (a constant a la #define, but with type checking and other nice properties).

However, in this case, using a const would not really bring this into play, but I'd just like preempt any possible problems if they tried to use a const to initialize an array in C.

_cashel said:
why would I be getting syntax errors there? It may be pretty obvious, but I'm obviously pretty new to this. I appreciate the help guys.

year, weight, etc. are just numeric constants - if you think of it that way, the line would read something like this:

Code:
365, 150, 15/365;

They're unqualified by anything. And the C grammar doesn't have a rule for number, number, number...; as AFAIK. :p
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
BillLeeLee said:
They're unqualified by anything. And the C grammar doesn't have a rule for number, number, number...; as AFAIK. :p
Actually, it does. It's a meaningless statement, and should just be deleted so it doesn't cause confusion. The comma operator evaluates each operand, and results in the last operand. So

Code:
int n;
n = foo(), 35, 11, bar(), 13;

calls foo(), calls bar(), then assigns 13 to n.

Code:
365, 150, 15/365;

evaluates to 0, then doesn't do anything with the result, so it doesn't do anything.
 

BillLeeLee

[H]F Junkie
Joined
Jul 2, 2003
Messages
13,486
mikeblas said:
Actually, it does. It's a meaningless statement, and should just be deleted so it doesn't cause confusion. The comma operator evaluates each operand, and results in the last operand. So

Code:
int n;
n = foo(), 35, 11, bar(), 13;

calls foo(), calls bar(), then assigns 13 to n.

Code:
365, 150, 15/365;

evaluates to 0, then doesn't do anything with the result, so it doesn't do anything.

Ah, I see, but should a line like

Code:
year, beer_calories, weight;

compile correctly then? If I try that in gcc (3.4.6) it fails to build simply with "Syntax error before numeric constant."

G++ 3.4.6 will spit out "expected unqualified-id before numeric constant." I was merely going by what my compiler would be doing and what I was thinking should happen, but I don't have other compilers to reference in this case.

Still not a necessary line though, in any case.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
_cashel said:
you've pretty much lost me on those. How do I enable error checking. By specifying units, you mean like how much beer, etc.? The rest I don't really know where to start. I put the PAUSE in there because it was closing the program out immediately, before you could see the results.
You don't enable error checking; you write code to deal with errors that might come up. What if you call scanf() and someone types in "seven" instead of "7"? What if they press CTRL+Z or CTRL+D instead of typing a number? What does your program do when it doesn't get the input it expects? You need to read the docs for the functions you're calling, decide what errors the make, figure out which ones you need to handle, and code accordingly.

I don't mean how much beer; I mean what unit of money, or weight, your program is dealing with.

Not to harsh your deal, but you put PAUSE in there because somebody told you to and you don't know what it does. When you say "it was closing the program out"; what is "it"? What was colosing the program out? I expect that it's your IDE, and that you've never run the program from a command shell. What happens when you do?

What if the user redirected stdin to your program? What does the call to "pause" do then?

Bottom line is that you don't need that call; there's more correct ways to solve your problem.

If you turned this in as is, and I was your professor, you'd get a "C" or a "D" for the assignment. I'd flunk you if I logged in here and saw how much help you got and still produced that result.

_cashel said:
What exactly do I need to change (or add) to make it work?
This is too specific a request for help, I think. We can't do your homework for you completely. we've held your hand through the whole exercise, and this is only your first project. When things get harder, on the second (or tenth!) assignment, will you be ready to do it yourself?

You've got a bug, so start debugging. You ask for other user input and don't segfault, so what's different about your new code compared to the code that works?
 

BillLeeLee

[H]F Junkie
Joined
Jul 2, 2003
Messages
13,486
Okay, I retract my previous post - I just got home and tested it with Visual Studio 2005 and STL's distro of GCC (3.4.2) and the year, beer_calories, weight; line compiles just fine.

Perhaps I have stumbled upon a GCC 3.4.6 bug.
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
Ok weve been working on this and this is what we have so far:
Code:
#include <stdio.h>

#define days 365
#define calories 150

int main()
{
    float beer, total_beers, cost, total_cost, total_calories, total_weight;
    int leap;
    
    //Get data from user        
    printf("On average, how many beers will you consume each day? ");
    scanf("%f", &beer);
    printf("On average, how much will you pay for each can of beer? ");
    scanf("%f", &cost);
    printf ("Is it a leap year? (1 = yes, 2 = no)");
    scanf("%d", &leap);

    if (leap == 1)  
    leap = days + 1; 
    else 
    leap = days;
    
    //Calculate amount of beers consumed a year
    total_beers = beer* leap;
    total_calories = total_beers* calories;
    total_weight = total_beers* 15.04/leap;
    total_cost = total_beers* cost;
    
    //Print out the amount consumed a year.
    printf("That is approximately %.2f beers in one year.\n", total_beers);
    printf("In one year, you will consume approximately %.2f calories from beer alone.\n", total_calories);
    printf("Without diet or exercise to counter these calories, you can expect to gain %.2f pounds from drinking that much beer this year.\n", total_weight);
    printf("You will spend approximately $%.2f on beer this year.\n", total_cost);
    system("PAUSE");
    return 0;
}

If you compile and run this, you are supposed to use the numbers '0.285' for beers and '0.50' for cost when inputing it into the working program. You're also supposed to choose 2 for a non leap-year, but also be able to chose 1 and have it calculate for a leap year.

Is it better to use %d for the choosing printf/scanf statement? Or is it better to keep it using %f as a float? We had the leap inside the float list before but it did the same thing.

Also:

Code:
    if (leap == 1)  
    leap = days + 1; 
    else 
    leap = days;

We think something is wrong witht his if/else statement. Inside the final printf's, only the first and third printf's are giving off-values of 4.29 and 104.03 when they are supposed to output 104.02 and 2.27. Here is a sample output:

On average, how many beers will you consume each day?
0.285
On average, how much will you pay for each can of beer?
0.50
Is it a leap year? (1 = yes, 2 = no)
2
That is approximately 104.02 beers in one year.
In one year, you will consume approximately 15603.75 calories from beer alone.
Without diet or exercise to counter these calories, you can expect to gain 4.27 pounds from drinking that much beer this year.
You will spend approximately $52.01 on beer this year.

<3
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
define the variables as "double" not "float" and see if that makes a difference. Given that the error is rather small, it may be a rounding issue.

Doubles are 'generally' more precise than floats, but it depends on the implementation of the system and the compiler.

I tought you were at 2.29 and blah not, 4.27... oops.

me = blind at times.
 

Shadow2531

[H]ard|Gawd
Joined
Jun 13, 2003
Messages
1,670
Do you have to do leap years exactly, or can you just go with a year being 365.242199 days?

mikeblas said:
You don't enable error checking; you write code to deal with errors that might come up. What if you call scanf() and someone types in "seven" instead of "7"? What if they press CTRL+Z or CTRL+D instead of typing a number? What does your program do when it doesn't get the input it expects?

From, http://www.cplusplus.com/ref/cstdio/scanf.html scanf returns an int of the number of items sucessfully read or EOF ( -1 for me) if an error occured.

Would checking the result to see if it's less than 1 like below be sufficient?
Code:
#include <stdio.h>

int main() {
    float beers, cost;
    int check;
    check = scanf("%f", &beers );
    if ( check < 1 ) {
        printf("\nError in beers input\n");
        return 1;
    }
    check = scanf("%f", &cost);
    if ( check < 1 ) {
        printf("\nError in cost input\n");
        return 1;
    }
    return 0;
}

Or

Code:
#include <stdio.h>

int main() {
    float beers, cost;
    if ( 1 > scanf("%f", &beers ) ) {
        printf("\nError in beers input\n");
        return 1;
    }
    if ( 1 > scanf("%f", &cost) ) {
        printf("\nError in cost input\n");
        return 1;
    }
    return 0;
}
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
drizzt81 said:
define the variables as "double" not "float" and see if that makes a difference. Given that the error is rather small, it may be a rounding issue.

Doubles are 'generally' more precise than floats, but it depends on the implementation of the system and the compiler.
float vs. double doesn't matter here. There's logic and math errors in the program, and math errors in the reference data. Why blame the tools before checking the technique or the specification?
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
mikeblas said:
float vs. double doesn't matter here. There's logic and math errors in the program, and math errors in the reference data. Why blame the tools before checking the technique or the specification?

I get your point and you are correct.

So let's see, if we assume that there are 365 days/(non leap year) and we drink .285 beers/ day, we should be drinking 104.25 beers/ year. Now, if we were to round that, there are two options according to wiki and that is pretty much in line with what I remember from math/ science classes:

1. we say that we round 0,1,2,3,4 down and up in other cases.
2. we round 0,1,2,3,4 down, 6,7,8,9 up and if the last digit is a 5, we only round up if it is followed by any non-zero digits.

Apparently, the thread starter's rounding method is the first.

I have close to no idea how the other value is calculated. I assume that this is given in his assignment and is either:
15 / 365 * number_of_beers 15 / 365 * 104.25 ~= 4.275

-or-
15.04 / days_per_year * number_of_beers 15.04 / 365 * 104.25 ~= 4.2864


I guess the OP should check his spelling. On a sidenote, if you are concerned about the I/O functionality and the if statement not working, why don't you check your program logic by implementing the calculation part first? Hardcode the 'test' values first and then run the program to see if the output is correct. Once you have that debugged, you can then go on to work on the input, then finalize with the if statement. Also, learning to use a debugger is a valuable experience.
 

pedant

2[H]4U
Joined
Feb 1, 2001
Messages
3,266
Whilst constructive in the long term, I think you are being a little harsh on them for their second program ;)
However, when I learnt C at university, they had an automated testing program that would look for a 'reasonable' amount of comments in the code, check the program produced the correct output for given inputs, etc.

My only useful input here is that when you start having methods longer than a page (this includes main), it is time to break them up into smaller methods. Pass the required data through in the method call - not as global variables.
 

Dillusion

Supreme [H]ardness
Joined
Oct 21, 2003
Messages
5,465
ok well, the programs were due tonight at 3 AM. it has been submitted by both me and castel. Time will tell what we get on the assignment, but I am confident it wont be too bad ;)

Thanks to all of you for your help, im sure i'll have questions in every assignment, but you guys have helped more than enough....This program was very time sensetive so I needed answers quick, hopefully i'll have more time to read the book on the next assignment, due on sept. 24th :D

C is a very complicated language and is nothing like the HTML i taught myself.
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
Dillusion said:
ok well, the programs were due tonight at 3 AM. it has been submitted by both me and castel. Time will tell what we get on the assignment, but I am confident it wont be too bad ;)

Thanks to all of you for your help, im sure i'll have questions in every assignment, but you guys have helped more than enough....This program was very time sensetive so I needed answers quick, hopefully i'll have more time to read the book on the next assignment, due on sept. 24th :D

C is a very complicated language and is nothing like the HTML i taught myself.
From my perspective:

HTML is -as the name says- a markup language, i.e. most of the code describes the content and nothing more. From the little HTML experience I have, there isn't even an assignment operator.

C on the other hand is a programming language. While a programming language describes itself as well (int j says that whatever I call j shall be interpreted as an integer), it can perform operations, such as assigning values to what I call j.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
pedant said:
Whilst constructive in the long term, I think you are being a little harsh on them for their second program ;)
Perhaps. But this poster seems far more interested in asking for help than checking a manual; and far more excited about the rush party than the class.
 
Top