| | |
Using loop functions
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2007
Posts: 6
Reputation:
Solved Threads: 0
When using loop functions or statements which loop statement do you prefer to use that would be easy for a beginner like me to use:
The do while loop, for loop, or while loop. I ask because I am to re-write my code from using the If statement to using one of the stated loop functions into my code. I am writing a coke machine program, I haven't started re-writing it yet I wanted to know which one of the stated loop functions would be practical for a beginner like me to use. If you need to see my code here it is. It is written in If statements, I haven't started on the loop functions yet.
The do while loop, for loop, or while loop. I ask because I am to re-write my code from using the If statement to using one of the stated loop functions into my code. I am writing a coke machine program, I haven't started re-writing it yet I wanted to know which one of the stated loop functions would be practical for a beginner like me to use. If you need to see my code here it is. It is written in If statements, I haven't started on the loop functions yet.
c Syntax (Toggle Plain Text)
#include <stdio.h> int main() { char drinkchoice; float insert; float money = 1.00; float totalinsert; printf("\nHow much do you wish to insert: "); scanf("%f", &insert); if(totalinsert < money) { printf("Please insert more money: "); scanf("%f", &insert); totalinsert = insert + totalinsert; } if(totalinsert < money) { printf("Please insert more money: "); scanf("%f", &insert); totalinsert = (insert + totalinsert); } if(totalinsert < money) { printf("Please insert more money: "); scanf("%f", &insert); totalinsert = (insert + totalinsert); } if(totalinsert < money) { printf("Please insert more money: "); scanf("%f", &insert); totalinsert = (insert + totalinsert); } if(totalinsert == money) { printf("\nWhat would you like."); printf("\nPress 1 for coke\nPress 2 for sprite\nPress 3 for drpepper: "); scanf("%c", &drinkchoice); totalinsert = (totalinsert == insert); getchar(); } if(drinkchoice == '1') { printf("here is you coke"); scanf("%c", &drinkchoice); getchar(); } if(drinkchoice == '2') { printf("here is your sprite"); scanf("%c", &drinkchoice); getchar(); } if(drinkchoice == '3') { printf("here is your dr pepper"); scanf("%c", &drinkchoice); getchar(); } getchar(); return 0; }
Last edited by WaltP; Jun 3rd, 2007 at 2:44 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
Short answer would be to have a while loop controlled by the totalinsert and money variables
C Syntax (Toggle Plain Text)
while( totalinsert < money) { /* ask for money here, add it to total insert */ } /* you get here when you have enough totalinsert */
![]() |
Similar Threads
- One Dimensional Array (C++)
- Output problems (C++)
- Very basic de/encrypt program (Python)
- matlab help : function, global, and ode45 (Legacy and Other Languages)
- header file/ classes (C++)
- structures and functions woohoo! (C++)
Other Threads in the C Forum
- Previous Thread: car park C programming
- Next Thread: Can anyone help me with my source code?
Views: 2742 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
#include * array arrays asterisks binarysearch calculate changingto char cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkwinlinux hacking hardware histogram homework inches include incrementoperators input iso kernel keyboard km lazy linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue mysql number opendocumentformat opensource overwrite owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string structures student systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi





