Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

repeat choosing color in c with do while

i want to repeat choosing a color until i press n when the program ask me if i want to keep choosing
PHP Code

<?php 
#include <stdio.h>

int main(){
    
int color ;
    
char rep[10];
    
printf(" donner le color 1/red 2/blue 3/green \n");
    
scanf("%d",&color);
    do{
    
    switch(
color) {
        case 
1 : printf("red");
        break;
        case 
2 : printf("blue");
        break;
        case 
3 : printf("green");
        break;
        default : 
printf("you didn't chose anything'");
    
    }
    
printf("\n do you want to continue chosing ? \n");
    
scanf("%s",&rep);
}while(
rep == "y");
} 
Writeln('Give me a Target >');
[Image: giphy.gif]
somthing like
PHP Code

<?php 
... 
didnt test it

edit - will post code later if i get free time
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
Why don't you use an endless loop and exit at a signal then ?

The tricky part is where you'd be assigning a single char to int along with comparing a char to int.. That causes unexpected behavior.

But my "partial" solution would be like this:

PHP Code

<?php 
#include <stdio.h>

int main(int argc, char* argv[])
{
        
int color;
        while(
1)
        {
                
printf("Choose a color: ");
                
scanf("%d", &color); // Save the number 

                
switch(color)
                { 
// Print our response 
                        
case 1:
                                
printf("\nred\n");
                                break;
                        case 
2:
                                
printf("\nblue\n");
                                break;
                        case 
3:
                                
printf("\ngreen\n");
                                break;
                }

                
// Exit loop on 0 otherwise repeat loop. 
                
if(color == 0) break;
        }
} 

Now... If I am understanding what you want to do correctly. But I have no idea what you want to do with char rep[10];


Edit:
A slightly modified version of the code above and made into a do - while loop.

PHP Code

<?php 
#include <stdio.h>

int main(int argc, char* argv[])
{
        
int color;
        
char rep = 'y';
        do
        {
                
printf("Choose a color: ");
                
scanf("%d", &color); // Save the number 

                
switch(color)
                { 
// Print our response 
                        
case 1:
                                
printf("\nred\n");
                                break;
                        case 
2:
                                
printf("\nblue\n");
                                break;
                        case 
3:
                                
printf("\ngreen\n");
                                break;
                }

                
printf("Keep choosing ? ");
                
scanf(" %c", &rep); /* scanf does not skip any leading whitespaces. Having a space in front of %c makes it skip them */

        
}
        while(
rep != 'n');
} 
he is learning and practicing scanf
you can use default for other key
PHP Code

<?php 
int color
;
   
printf(" donner le color 1/red 2/blue 3/green other/exit\n");
   while(
1) { 
      
scanf("%d", &color);
      switch(
color) {
         case 
1 : printf("red\n");
            break;
         case 
2 : printf("blue\n");
            break;
         case 
3 : printf("green\n");
            break;
         default : exit(
1);
      }
   } 
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Mar 17, 2021 01:12 PM)HFDone Wrote:  he is learning and practicing scanf
you can use default for other key
PHP Code

<?php 
int color
;
   printf(" donner le color 1/red 2/blue 3/green other/exit\n");
   while(1) { 
      scanf("%d", &color);
      switch(color) {
         case 1 : printf("red\n");
            break;
         case 2 : printf("blue\n");
            break;
         case 3 : printf("green\n");
            break;
         default : exit(1);
      }
   } 

He can, yes as showed in my edit with a do - while loop.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by prodigyhfSep 1, 2017 05:22 PM 0 223 Sep 1, 2017 05:22 PM
Last Post: prodigyhf
[Shell] by StartHeartsJul 16, 2016 06:59 AM 3 216 Jul 16, 2016 06:59 AM
Last Post: b00st3d
[C#] by ze-sladeMay 12, 2016 12:59 PM 4 143 May 12, 2016 12:59 PM
Last Post: ze-slade