Solved

a newcomer to c programming needs help

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now