Hack Forums
[C] repeat choosing color in c with do while - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] repeat choosing color in c with do while (/showthread.php?tid=6144920)



repeat choosing color in c with do while - blackcoder00 - Mar 16, 2021

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");
} 



RE: repeat choosing color in c with do while - HFDone - Mar 16, 2021

somthing like
PHP Code

<?php 
... 
didnt test it

edit - will post code later if i get free time


RE: repeat choosing color in c with do while - Loki123 - Mar 17, 2021

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');
} 



RE: repeat choosing color in c with do while - HFDone - Mar 17, 2021

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);
      }
   } 



RE: repeat choosing color in c with do while - Loki123 - Mar 17, 2021

(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.