Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

a newcomer to c programming needs help

Posted on 2004-04-05
5
Medium Priority
?
359 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 80 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

721 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