Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi, I am trying to write a program which asks the user if they would like the alphabet written in upper or lower case after they make the choice the program with then print the chosen choice, here is what I have so far...
C#
#include <stdio.h>

int main ()
{
    char string1[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char string2[27] = "abcdefghijklmnopqrstuvwxyz";
    int value = 0;

    printf("To print the alphabet in\n");
    printf("uppercase enter 0\n");
    printf("lowercase enter 1\n");
    scanf("%i",&value);

    if (value != 0||1)
        printf("Invalid entry please enter only 0 or 1");
    else if (value = 0)
        printf("%s", string1);
    else if (value = 1);
        printf("%s", string2);

    return 0;
}
i am just getting started with loops and if statments and have been trying to get this working for a while.
Posted
Updated 18-Dec-09 7:33am
v2

The line that says:
scanf("%i\n",&value);
Should say:
scanf("%i",&value);
 
Share this answer
 

#include <stdio.h>
int main ()
{

char string1[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char string2[27] = "abcdefghijklmnopqrstuvwxyz";
int value = 0;

printf("To print the alphabet in\n");
printf("Uppercase enter 0\n");
printf("lowercase enter 1\n");
scanf("%i",&value);

while ((value != 0) && value != 1)
{
printf("Invalid entry please enter only 0 or 1\n");
scanf("%i\n",&value);
}

if (value == 0)
{
printf("%s", string1);
}
else if (value == 1)
{
printf("%s", string2);
}
return 0;

}

this is what i got which works to an extent but for some reason after entering e.g 9 then 0 it needs me to enter another int so for some reason which i cant see

 
Share this answer
 

What you are trying to do is more of a logical issue. You are attempting to catch input when it is a 0 or a 1. Your logic 'Looks' like good logic, but really it isn't.

Let me explain what you are saying:

if (value != 0||1)

What this means is this, If the variable value is not equal to undefined then do this.

What you mean is this,

If the variable value is not equal to 0 AND the variable value is not equal to 1 then do this.

Simply put you are confusing the || or comparitor. You need to have 2 tests, but you only have one.

As to the Loop, here is an example of what I think you wanted.

#include

"stdafx.h"

int _tmain(int argc, _TCHAR* argv[])

{

//Create two string arrays, one upper, one lower.

char string1[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char string2[27] = "abcdefghijklmnopqrstuvwxyz";

//Create an integer to catch the user input.

int value = 0;

// create a loop to prompt the user.

While (value == 0)

{

printf(To print the alphabet in\n");

printf("uppercase enter 1\n");

printf("lowercase enter 2\n");

scanf("%i",&value);

} //end of while loop

// Test the input, if the input is not 1 and input is not 2,

if ((value != 1)&&( value != 2))

{

printf("Invalid entry please enter only 0 or 1");

}

// If the input is a one, send the Upper Case String.

else if (value == 1)

{

printf("%s",string1);

}

// If the input is two, send the Lower Case String.

else if (value == 2)

{

printf("%s",string2);

}

//exit the program.

return 0;

}

 
Share this answer
 

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