Hello everyone, I'm new to learning C language and was wondering if anyone with more experiance could help me out.
I only have about 20 hours at most of self learned C programming under my belt so bare with my stuff here.
Once I realized I knew enough to toss together a simple program that actully does somthing other then say "blah blah blah" i made a pound/kilogram converter, anyway the point of my post is I used while loop statments in my code and shortly after started learning about for loop statments, I thought I understood how to change my while loops to for loops but apparently I don't, when I change it to use for loops enstead of while loops when i run my program and select 2 it goes to convert pounds to kilograms although it should be converting kilograms to pounds like it does when I use the while loops.
Anyway heres my code and how I thought I was supposed to change it and my question is at the bottom
Code:#include <stdio.h> int main() { float a, b; a = 0; b = 0; printf("\t\tPound to Kilogram converter by Dustin Silver\n"); printf("\nType 1 to convert pounds to kilograms\nType 2 to convert kilograms to pounds\nthen press enter:"); scanf("%f", &b); while (b < 3) { if (b > 1) { while (b > 1) { printf("\nEnter amount of kilograms to be converted to pounds then press enter:\n"); scanf("%f", &a); printf("%f kilogram(s) Kgs = %f pound(s) Lbs\n", a, (a * 2.20462)); printf("\nCreated & proudly produced by Dustin Silver ;)\n"); } } printf("\nEnter amount of pounds to be converted to kilograms then press enter:\n"); scanf("%f", &a); printf("%f pound(s) Lbs = %f kilogram(s) Kgs\n", a, (a / 2.20462)); printf("\nCreated & proudly produced by Dustin Silver ;)\n"); } return 0; }
Here is how I thought it was supposed to be changed. (if I wanted for loops enstead of while loops)
ORCode:#include <stdio.h> int main() { float a, b; a = 0; printf("\t\tPound to Kilogram converter by Dustin Silver\n"); printf("\nType 1 to convert pounds to kilograms\nType 2 to convert kilograms to pounds\nthen press enter:"); scanf("%f", &b); for (b=0; b<3; b+0) if (b > 1) { for (b=0; b>1; b+0) printf("\nEnter amount of kilograms to be converted to pounds then press enter:\n"); scanf("%f", &a); printf("%f kilogram(s) Kgs = %f pound(s) Lbs\n", a, (a * 2.20462)); printf("\nCreated & proudly produced by Dustin Silver ;)\n"); } printf("\nEnter amount of pounds to be converted to kilograms then press enter:\n"); scanf("%f", &a); printf("%f pound(s) Lbs = %f kilogram(s) Kgs\n", a, (a / 2.20462)); printf("\nCreated & proudly produced by Dustin Silver ;)\n"); return 0; }
but neither of these two codes work like the first one as explained earlierCode:#include <stdio.h> int main() { float a, b; a = 0; printf("\t\tPound to Kilogram converter by Dustin Silver\n"); printf("\nType 1 to convert pounds to kilograms\nType 2 to convert kilograms to pounds\nthen press enter:"); scanf("%f", &b); for (b=0; b<3; b+0) { if (b > 1) { for (b=0; b>1; b+0) { printf("\nEnter amount of kilograms to be converted to pounds then press enter:\n"); scanf("%f", &a); printf("%f kilogram(s) Kgs = %f pound(s) Lbs\n", a, (a * 2.20462)); printf("\nCreated & proudly produced by Dustin Silver ;)\n"); } } printf("\nEnter amount of pounds to be converted to kilograms then press enter:\n"); scanf("%f", &a); printf("%f pound(s) Lbs = %f kilogram(s) Kgs\n", a, (a / 2.20462)); printf("\nCreated & proudly produced by Dustin Silver ;)\n"); } return 0; }
Can someone please help me convert the while loop staments in the top piece of code to for loop statments, any help would be very very appreciated.
Also I have another small piece of code I'm trying to convert from for loops to while loops to help me better understand the concept of for looping but after I make the changes the results are not what I'm expecting, if anyone could help me convert this piece of code the same way as I need the other one converted (but opposite) it would be very very appreciated.
thanks,Code:#include <stdio.h> int main() { int a[5]; int i; for (i=0; i<5; i++) a[i] = i; for (i=0; i<5; i++) printf("a[%d] = %d\n", i, a[i]); return 0; }
Dustin



1Likes
LinkBack URL
About LinkBacks



