Solved

a newcomer to c programming needs help

Posted on 2004-04-05
5
339 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

776 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