C programming help :D

Associate
Joined
28 Feb 2009
Posts
519
Im trying to make a confirm function where the user types either Y for yes or N for no to confirm that they wish to make changes. My code is as follows however it doesn't work.


if((strcmp(array[0], "export")==0))
{
printf("\nPlease Confirm you wish to change [%s] to %s ",myarg[1], myarg[2]);
conf = 0;
end = 0;
while(end != 1)
{
printf("Type Y for Yes or N for No");
temp = getchar();
if (temp = "Y")
{
con = 1;
end = 1;
}
if (temp = "N")
{
con = 0;
endk = 1;
}

}
}
 
if (temp = "Y")
if (temp = "N")

You need to use double equals:
if (temp == "Y")
if (temp == "N")
 
if (temp = "Y")
if (temp = "N")

You need to use double equals:
if (temp == "Y")
if (temp == "N")

The reason behind this is that = is the assignment operator. It gives the preceding variable a value, so when you type the following:

Code:
if (temp = "N")

Although what you are attempting to do is say "if the variable temp is equal to the letter N", what the compiler sees is an if, then you setting the value of "temp" to "N". == is the "equal to" operator (in C at least), and should be used for making comparisons.

This can become a little bit confusing when you look at other language. For example, at work, we use VBScript a lot on the system and there = is used both for assignment and for comparing equality, so you just have to be careful with what you are working with.
 
Actually if you're using getchar then you need to change it to single quotes, e.g.

if (temp == 'N')

Double quotes creates a literal string, which is completely different to the integer character returned by getchar...
 
Back
Top Bottom