Solved

a newcomer to c programming needs help

Posted on 2004-04-05
5
357 Views
Last Modified: 2010-04-17
i have just started programming in c and i am trying to make a really basic logon program. i started with a really basic bit of code that i will later add to to make a finished program.

here's the code:

#include stdio.h

int main()
{
char var1;
printf("enter your name\n");
printf("> ");
scanf("%s",var1);
printf("\n");
if(var1="ico2")
{
printf("correct");
}
else
{
printf("wrong");
}
}

but whatever i write at the input it still tells me i am correct even if i am not.
0
Comment
Question by:ico2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
Droby10 earned 20 total points
ID: 10758800
the problem lies in your comparison operators.

= is an assignment operator.  so when you say

if(var1="ico2")
you are setting var1 = "ico2" and the if (expr) is evaluating that assignment as a boolean (always true).

if you are wanting to compare two values, then you would use ==

ie. if(var1=="ico2")

and to complicate things to one more degree, you are attempting to compare string data.  a statement such as the above example would actually be testing if the address of the identifier var1 is equal to the address for the literal.  which is generally not intended use.  instead you must compare character by character until the end of the string (test true) or until a difference is found (test false).  luckily there are standard functions already written for this, the most applicable being strcmp.

if(strcmp(var1, "ico2")==0)
{
  /* true part */
} else {
 /* false part */
}

the only other thing i see wrong syntactically/functionally is that you have declared var1 as a single character rather than an array of characters (or a string), and then attempted an unsafe read into that variable.

#include <stdio.h>
#include <string.h>

int main()
{
  char var1[12]; /* allocate 12 characters for our name string */
  printf("enter your name\n");
  printf("> ");
  scanf("%11s",var1); /* only scan up to 11 characters into our name string */
  printf("\n");
  if(strcmp(var1, "ico2")==0) /* note the use of strcmp for string comparison and == for numeric comparison */
  {
    printf("correct");
  }
  else
  {
    printf("wrong");
  }
  return 0; /* we declared main to return a value of int type */
}
0
 
LVL 4

Expert Comment

by:oumer
ID: 10758825
I am surprised that your compiler didn't complain because you are using

if(var1="ico2")
{
printf("correct");
}
else
{
printf("wrong");
}

var1="ioc2" always returns true as you are assigning a value to some other,
If you want to check the equality of things you should use == instead (two = signs without any space)

By the way, you should put your c questions in the programming->languages->c section to get prompt answers
0
 
LVL 1

Author Comment

by:ico2
ID: 10758891
hmmm i already tried that and it always says wrong.
how can i get it to correctly produce "correct" or "wrong" based on if the value input is equal to "ico2"
0
 
LVL 1

Author Comment

by:ico2
ID: 10758910
sorry droby10 i didn't see your answer.
i will try it out now.
0
 
LVL 1

Author Comment

by:ico2
ID: 10758926
wow it works!!!

thanks so much.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question