Solved

a newcomer to c programming needs help

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countX 22 80
firstChar challenge 13 106
Hide vba in gp 7 79
C# application error "Parameter Is Not Valid". 3 75
This article will show, step by step, how to integrate R code into a R Sweave document
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

947 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

20 Experts available now in Live!

Get 1:1 Help Now