Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 21st, 2005, 12:25 AM   #1
Ramlag
Newbie
 
Join Date: Mar 2005
Posts: 18
Rep Power: 0 Ramlag is on a distinguished road
Any ideas what i've done wrong? (Newb C Programmer) Code included

Dont worry, this isnt a homework assignment or similar (reading forums you see people tend to just post stuff and ask you to do their homework for them).

Theres a tutorial for learning C over at howstuffworks.com, one of the "set yourself a task" things it asks you to do is to modify a basic formula chart (its celcius to farenheit in this case) by making the user able to define the starting, ending and increment values of the chart. Anyhow i did all of that individually but for some reason i just put this together and it screwed up, heres the code.

#include <stdio.h>

int main()
{
    float a, b, c, d, e;
    a = 0; //A = Starting Value
    b = 0; //B = Increment value
    c = 0; //C = Ending Value
    d = 0; //d = On / Off Switch
    e = 0; //e = Ready Switch
    
    printf("Insert the starting value.\n");
    scanf("%6.2f", a);
    printf("Insert the ending value.\n");
    scanf("%6.2f", c);
    printf("Insert the increment value.\n");
    scanf("%6.2f", b);
    printf("You have selected a starting value of %6.2f,\nAn ending value of %6.2f \nand an increment value of %6.2f.\n", a, c, b);
    e = 1;
    
    while ((a <= b) && (e == 1))
    {
	if ((a >= 98.6) && (d == 0))
        {
            printf("%6.2f degrees F = %6.2f degrees C\n",
                98.6, (98.6 - 32.0) * 5.0 / 9.0);
                d = 1;
        }
        printf("%6.2f degrees F = %6.2f degrees C\n",
            a, (a - 32.0) * 5.0 / 9.0);
        a = a + b;
    }
    return 0;
}

Ive added in some comments to explain a little

The first test was to figure out an alternate way of getting the 98.6 degree thingy of working (hence my "d" switch) that worked fine within the code.

It just executed the code the second i typed the first value, so i figured id need some way of stopping the code till i input all the values (hence the "e" switch, still didnt work). I dont wanna re-write the whole thing (im sure id fix it) i just want to understand what it is i've left out, any ideas?

Edit: Better add this in, running the script produces an infinite loop (i think its called) which means that its just gonna keep printing the same thing over and over.

Last edited by Ramlag; Mar 21st, 2005 at 12:35 AM.
Ramlag is offline   Reply With Quote
Old Mar 21st, 2005, 1:12 AM   #2
codetaino
Programmer
 
codetaino's Avatar
 
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 13 codetaino is on a distinguished road
my comments are inside the code... labled codetaino
#include <stdio.h>

int main()
{
	//codetaino: next time use more descriptable variables
    float a, b, c, d, e;
    a = 0; //A = Starting Value
    b = 0; //B = Increment value
    c = 0; //C = Ending Value
    d = 0; //d = On / Off Switch
    e = 0; //e = Ready Switch
    
	//codetaino: you missed the and symbol before the variables in the scanfs
	//               also you cannot use 6.2 on the scanf thats for the printf
    printf("Insert the starting value.\n");
    scanf("%6f", &a);
    printf("Insert the ending value.\n");
    scanf("%6f", &c);
    printf("Insert the increment value.\n");
    scanf("%6f", &b);
    printf("You have selected a starting value of %6.2f,\nAn ending value of %6.2f \nand an increment value of %6.2f.\n", a, c, b);
    e = 1;
    
	//codetaino: this while does not let me have an initial value of 1 and a 
	//           increment of 2 its that ok?! also it will let me use a negative increment
    while ((a <= b) && (e == 1))
    {
		//codetaino: this if will be executed just once when an temp is higer or equal to 98.6
		//           also the outside printf will be executed in that case. its not enclosed in an else
		if ((a >= 98.6) && (d == 0))
        {
            printf("%6.2f degrees F = %6.2f degrees C\n",
                98.6, (98.6 - 32.0) * 5.0 / 9.0);
                d = 1;
        }
        printf("%6.2f degrees F = %6.2f degrees C\n",
            a, (a - 32.0) * 5.0 / 9.0);
        a = a + b;
    }
	//codetaino: when do you used the ending value (your c variable)???
	//codetaino: my recomendation is that you rethink your program, try to
	//           fix it, if still does not work ask your doubts again. Also
	//           try reading other tutorial. (scanf, printf, ifs)
    return 0;
}

-codetaino
im back
__________________
"God bless u all" :)

Last edited by codetaino; Mar 21st, 2005 at 1:18 AM.
codetaino is offline   Reply With Quote
Old Mar 21st, 2005, 1:25 AM   #3
Ramlag
Newbie
 
Join Date: Mar 2005
Posts: 18
Rep Power: 0 Ramlag is on a distinguished road
Thanks, i just caught the &'s as i was reading through it. I tried changing the values to things like "incval" "startval" ect, but i thought thats what caused the error, perhaps i used the name of an inbuilt function by accident or made the names too long.

As for the 6.2f that was the format i was using for the numbers (script was using it by default, that confused the hell out of me because presumably that would allow for temperatures up to .... 999999.99c? I left the start / increment values at 6.2f because of that, and values d & e as floats.. cause it was easier =p

Oh, as for the c value... duh *slaps head* that should have been in the very first line of code, i changed it all back after i renamed it
 while ((a <= b) && (e == 1))
heh god that was a lot of mistakes, ill edit the hell out of it and test it again (then remake it if it works to make it neater).

====================
====================
Edit: Ok, heres the ammended code (changed 6.2's to 4'2s ect, put end value in right place, the problem i have i believe is delaying the "while" statement till after i have input all the data ... at least thats what i think i've done wrong.

#include <stdio.h>

int main()
{
    float a, b, c, d, e;
    a = 0; //A = Starting Value
    b = 0; //B = Increment value
    c = 0; //C = Ending Value
    d = 0; //d = On / Off Switch
    e = 0; //e = Ready Switch
    
    printf("Insert the starting value.\n");
    scanf("%4.2f", &a);
    printf("Insert the ending value.\n");
    scanf("%4.2f", &c);
    printf("Insert the increment value.\n");
    scanf("%3f", &b);
    printf("You have selected a starting value of %4.2f,\nAn ending value of %4.2f \nand an increment value of %3f.\n", a, c, b);
    e = 1;
    
    while ((a <= c) && (e == 1)) // i need to delay this till all the code above it has been executed
    {
	if ((a >= 98.6) && (d == 0))
        {
            printf("%4.2f degrees F = %4.2f degrees C\n",
                98.6, (98.6 - 32.0) * 5.0 / 9.0);
                d = 1;
        }
        printf("%4.2f degrees F = %4.2f degrees C\n",
            a, (a - 32.0) * 5.0 / 9.0);
        a = a + b;
    }
    return 0;
}

Last edited by Ramlag; Mar 21st, 2005 at 1:45 AM.
Ramlag is offline   Reply With Quote
Old Mar 21st, 2005, 11:13 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,428
Rep Power: 18 Ooble is on a distinguished road
In the scanf statements, change "%4.2f" to just "%7f" - you format it in the printf statement.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 21st, 2005, 11:41 AM   #5
codetaino
Programmer
 
codetaino's Avatar
 
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 13 codetaino is on a distinguished road
i still think this if is incorrect can you explain me what you want it to do?

	if ((a >= 98.6) && (d == 0))
        {
            printf("%4.2f degrees F = %4.2f degrees C\n",
                98.6, (98.6 - 32.0) * 5.0 / 9.0);
                d = 1;
        }
        printf("%4.2f degrees F = %4.2f degrees C\n",
            a, (a - 32.0) * 5.0 / 9.0);

-codetaino
__________________
"God bless u all" :)
codetaino is offline   Reply With Quote
Old Mar 21st, 2005, 3:39 PM   #6
Ramlag
Newbie
 
Join Date: Mar 2005
Posts: 18
Rep Power: 0 Ramlag is on a distinguished road
Well, in this script my starting value is meant to loop over and over and keep getting higher by the increment value.

	if ((a >= 98.6) && (d == 0)) // checks if the value goes 98.6 or above, if it does it places 98.6 down ONCE in the table, the d switch ensures it only happens once.
        {
            printf("%4.2f degrees F = %4.2f degrees C\n",
                98.6, (98.6 - 32.0) * 5.0 / 9.0);
                d = 1; // this switch turns it off and disables the script running this part more then once.
        }
        printf("%4.2f degrees F = %4.2f degrees C\n",
            a, (a - 32.0) * 5.0 / 9.0); / / this part is the main display part of the script, it prints out the current value of a and apllies the formula to display it in Centigrate as well.

Ooble, wouldnt i only need to enter %6f? Or does the decimal count towards that total too?

Last edited by Ramlag; Mar 21st, 2005 at 3:42 PM.
Ramlag is offline   Reply With Quote
Old Mar 21st, 2005, 4:32 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,428
Rep Power: 18 Ooble is on a distinguished road
The decimal counts.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 22nd, 2005, 2:11 AM   #8
codetaino
Programmer
 
codetaino's Avatar
 
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 13 codetaino is on a distinguished road
my point with your if is that when the number is higer than 98.6 it will execute both parts of the codes... once the part inside the if... and always the part outside the if... and im kidna sure you dont want that

-codetaino
__________________
"God bless u all" :)
codetaino is offline   Reply With Quote
Old Mar 22nd, 2005, 4:26 AM   #9
Ramlag
Newbie
 
Join Date: Mar 2005
Posts: 18
Rep Power: 0 Ramlag is on a distinguished road
yes i do, so it makes a loop ... the first If statement is only meant to be executed once, then the second If statement gets repeated over and over until the value of "c" is reached.... kinda... hell i dunno lol.
Ramlag is offline   Reply With Quote
Old Mar 22nd, 2005, 9:00 AM   #10
codetaino
Programmer
 
codetaino's Avatar
 
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 13 codetaino is on a distinguished road
in the if there is no else so if the number is higer than 98.6 in just one step on the loop both codes will be executed. to give you an example when the number is 150 in just one step both of this outputs will be shown:
98.60 degrees F = 37.00 degrees C
150.00 degrees F = 65.56 degrees C
and in the second step assuming an increment of 1 and a end valuo el 152
151.00 degrees F = 66.11 degrees C

as i undestand this is not what you want

hope i have explain myself correctly this time
-codetaino
__________________
"God bless u all" :)
codetaino is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC