![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 18
Rep Power: 0
![]() |
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 20th, 2005 at 11:35 PM. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 4
![]() |
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 12:18 AM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 18
Rep Power: 0
![]() |
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)) ==================== ==================== 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 12:45 AM. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
In the scanf statements, change "%4.2f" to just "%7f" - you format it in the printf statement.
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 4
![]() |
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" :) |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2005
Posts: 18
Rep Power: 0
![]() |
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 2:42 PM. |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
The decimal counts.
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 4
![]() |
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" :) |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Mar 2005
Posts: 18
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jan 2005
Location: Bayamon, Puerto Rico
Posts: 71
Rep Power: 4
![]() |
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" :) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|