Click here to Skip to main content
16,022,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i run this function, the printf will print two times

The output will come like

Reboot System
This will end the demo. Continue (Y/N)?Reboot System
This will end the demo. Continue (Y/N)?

tell me how to solve this...




<pre lang="C++"><pre>
void Reboot_System()
{
    while(1)
    {
        char option;
        printf("Reboot System\n");
        printf("\n This will end the demo. Continue (Y/N)?");
        scanf("%c", &option);
        if (option ==  'y' && option == 'Y')
        {
            system("reboot");
            printf("\n Reboot\n");
            while(1);

        }


    }



}


What I have tried:

I tried to print that only once but its not working...

C++
This is my complete code...

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>




void Reboot_System()
{
    while(1)
    {
        char option;
        printf("Reboot System\n");
        printf("\n This will end the demo. Continue (Y/N)?");
        scanf("%c", &option);
        if (option ==  'y' || option == 'Y')
        {
            system("reboot");
            printf("\n Reboot\n");
            while(1);
        }
    }
}

void Shutdown_System()
{
    char option;
    printf("Shutdown System\n");
    printf(".................");
    printf("\n This will end the demo. Continue (Y/N)?");
    scanf("%c", &option);
    if (option ==  'y' || option == 'Y')
    {
        system("shutdown");
        printf("shutdown\n");
    }
}

int main()
{
    char op;
    int n = 0;
    while(1)
    {
        printf("Power Management\n");
        printf("................\n");
        printf("Options:\n");
        printf("1.Reboot\n");
        printf("2.Shutdown\n");
       // printf("\n0.Quit\n");
        scanf(" \n%c", &op);
        switch(op)
        {
          case '1': Reboot_System();
                    break;
          case '2': Shutdown_System();
                    break;
         // case '0': exit(1);
           //         break;
          default : main();
                    break;

        }

    }
    return 0;

}
Posted
Updated 21-Jun-21 0:43am
v2

1 solution

C++
if (option ==  'y' && option == 'Y')

It cannot be equal to both values. It should be:
C++
if (option ==  'y' || option == 'Y') // 'y' OR 'Y'


[edit]
As is often the case this is caused by scanf. Add the following function:
C++
char getOption()
{
    char option[8];
    fgets(option, sizeof option, stdin);
    // NB should check for leading spaces ...

    return option[0];
}

And then replace your scanf calls with:
C++
option = getOption();


[/edit]
 
Share this answer
 
v2
Comments
Member 15253975 21-Jun-21 6:32am    
ok, i tried that but same output came...
[no name] 21-Jun-21 7:00am    
See my updated solution.
Member 15253975 21-Jun-21 6:34am    
This is my complete code...

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>




void Reboot_System()
{
while(1)
{
char option;
printf("Reboot System\n");
printf("\n This will end the demo. Continue (Y/N)?");
scanf("%c", &option);
if (option == 'y' || option == 'Y')
{
system("reboot");
printf("\n Reboot\n");
while(1);
}
}
}

void Shutdown_System()
{
char option;
printf("Shutdown System\n");
printf(".................");
printf("\n This will end the demo. Continue (Y/N)?");
scanf("%c", &option);
if (option == 'y' || option == 'Y')
{
system("shutdown");
printf("shutdown\n");
}
}

int main()
{
char op;
int n = 0;
while(1)
{
printf("Power Management\n");
printf("................\n");
printf("Options:\n");
printf("1.Reboot\n");
printf("2.Shutdown\n");
// printf("\n0.Quit\n");
scanf(" \n%c", &op);
switch(op)
{
case '1': Reboot_System();
break;
case '2': Shutdown_System();
break;
// case '0': exit(1);
// break;
default : main();
break;

}

}
return 0;

}
CPallini 21-Jun-21 7:01am    
5.
[no name] 21-Jun-21 7:03am    
Thanks. Why do people use scanf? It's the most useless function ever created (in my humble opinion).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900