memloop is right, you need to use && instead of ||.
Printable View
memloop is right, you need to use && instead of ||.
Ok dude did your problem solved or not what i suggested did you looked that or you are just doing argument here
Dear RockyMarrone,
ou
To answer your #8 thread :
I am trying to determine what kind of input the user wants to enter. User wants to enter in hexadecimal format or normal decimal format.
If the user wants to enter in Hex format then i will jump to the function that perform hexadecimal to decimal.
My idea is that user can only enter Y or y or N or n. otherwise it will keep prompting for the right key.
dear RockyMarrone,
To answer your #17 thread :
not quite really solve the problem. I am not doing an argument. I tried the program myself. It seem to go out of control in the later part of the program.
For example ,
user enter Y or y , the program should check if user enter Y or y , if so , then prompt user for the
second input( now in hexadecimal format ). Now the program did not stop to prompt the user
for the 2nd input. It just run over it ? i am not so sure.
But if i remove the first part ( that prompt user whether to enter in Hex or decimal format )
everything is fine again
I think you better post that code as it is now then, presuming you changed it (if you didn't, you need to -- hopefully it is already clear why II should be && there).
Rocky was wrong for saying you should use a char with getchar(). getchar() returns an int, and that is what you want. However, this will leave a character in the (std)input buffer, because what you actually type is:
Y\n
'\n' is from the enter key. So the next call to getchar() will process that, and skip right thru the next question! (The input buffer is persistant -- it does not go away. If you ask for one character and the user enters 2 or 5, the remain characters wait there on a first in, first out basis for a request, eg, with getchar or scanf()). If that is what is happening, insert an extra getchar() that does nothing:
This will eat the newline and clear the input buffer. Even better:Code:getchar();
This is guaranteed to eat all spare characters in the buffer up to a newline -- which logically, a '\n' will always be the last character in the stdin buffer.Code:int x;
while (x != '\n') x=getchar();
With your all post i think you need this kind of code
Code:#include <stdio.h>
#define SPACE ' '
int main() {
char choice = SPACE;
int hexadecimal = 0;
while (1) {
printf("Please enter your choice \n");
choice = getchar();
printf("choice === %c\n", choice);
if (choice == 'y' || choice == 'Y' ||\
choice == 'n' || choice == 'N')
break;
else
continue;
}
if (choice == 'y' || choice == 'Y') {
printf("Please enter a number in hexadecimal\n");
scanf("%x", &hexadecimal);
// Write your logic here
}
return 0;
}
Thank you very much ! The program is ok.
The following should be my final code.
Code:#include<stdio.h>
int romanize(int q[], int base)
{
int counter, temp;
printf("\nThe Romanize number is ");
if(base == 4)
{
// 4 digit
for(counter = 1; counter <= q[0]; counter++)
{
printf("m");
}
// 3 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("d");
for(counter = 1; counter <= temp; counter++)
{
printf("c");
}
}
// 3 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("c");
}
}
// 2 digit more than 5
if(q[2] >= 5)
{
temp = q[2] - 5;
printf("l");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[2] < 5)
{
for(counter = 1; counter <= q[2]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[3] >= 5)
{
temp = q[3] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[3] < 5)
{
for(counter = 1; counter <= q[3]; counter++)
{
printf("i");
}
}
}
if(base == 3)
{
// 3 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("d");
for(counter = 1; counter <= temp; counter++)
{
printf("c");
}
}
// 3 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("c");
}
}
// 2 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("l");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[2] >= 5)
{
temp = q[2] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[2] < 5)
{
for(counter = 1; counter <= q[2]; counter++)
{
printf("i");
}
}
}
if(base == 2)
{
// 2 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("l\n");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("i");
}
}
}
if(base == 1)
{
// 1 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("i");
}
}
}
}
main()
{
int i, j, n, ch, count, hexadecimal, decimal, number[10], temp[10];
int hex;
decimal=0, count=0, i=0, j=0;
printf("Enter in hexadecimal format ? (Y/N):");
hex = getchar();
printf("\n");
while(hex != 'Y' && hex != 'y' && hex != 'N' && hex != 'n')
{
getchar();
printf("Please enter the correct key! (Y/N):");
hex = getchar();
printf("\n");
}
//if yes then propmt user for hexadecimal input
if(hex == 'Y' || hex == 'y')
{
getchar();
printf("Please enter in hexadecimal format : ");
hexadecimal = getchar();
while(hexadecimal != '\n')
{
if('0' <= hexadecimal && hexadecimal <= '9')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - '0');
}
else if('A' <= hexadecimal && hexadecimal <= 'F')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - 'A')+10;
}
else if('a' <= hexadecimal && hexadecimal <= 'f')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - 'a')+10;
}
else
{
break;
}
hexadecimal = getchar();
}
printf("\nyour input number in decimal format is %d\n",decimal);
while(decimal != 0)
{
temp[i] = decimal%10;
decimal= decimal/10;
i++;
count++;
}
i=i-1;
for(j=0; j < count; j++)
{
number[j]=temp[i];
i--;
}
if(count < 5 ) // Handle number up to 9999 only
{
romanize(number, count);
}
else
printf("\nPlease enter hexadecimal number up to 270F only.\n");
}
else
{
ch = getchar();
printf("Please enter in decimal format : ");
ch = getchar();
while(ch!='\n')
{
if('0' <= ch && ch <= '9')
{
//This is to count the number of digit the user has enter//
n = 0;
n = n * 10;
n = n + (ch - '0');
number[i] = n; //Store each entered digit into array "intnumber[]"//
count++;
i++;
}
ch = getchar();
}
if(count < 5 ) // Handle number up to 9999 only
{
romanize(number, count);
}
else
printf("\nPlease enter number up to 9999 only.\n");
}
}