GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Aug-2004, 17:35
PhoenixG PhoenixG is offline
New Member
 
Join Date: Aug 2004
Posts: 1
PhoenixG is on a distinguished road

Making a Variable equal a letter in C


Hi

I'm trying to make a variable in my program equal a letter, i don't know how to do it, i'm sturggling to find out how you can do it, hence the posting here.

Here is the code, bare in mind its still a bit scrappy:

CPP / C++ / C Code:
#include <stdio.h>
int main(int argc, char **argv)

{
  int i, k=0;
  int age;
  char name[10];
  char sex[1];
  char spec[1];
  FILE *myfile;
  myfile = fopen(argv[1], "r");
  

  if (argv[1] == NULL)
  {
    printf("No command line arguements were found.\n");
    printf("To gain access, enter the name of the text file to open.\n");
    return 0;
  }

  else if (argc > 2)
  {
    printf("Too many arguements entered.\n");
    printf("Please only enter one arguement.\n");
    return 0;
  }

  else if (myfile == NULL)
  {
    printf("Cannot open %s.\n", argv[1]);
    return 0;
  }

  else if (fgetc(myfile) == EOF)
  {
    printf("The file is empty.\n");
    return 0;
  }

  else while ((i = fgetc(myfile)) != EOF)
  {
  
   fscanf(myfile, "%d%s%s%s", &age, name, sex, spec);
   printf("%d %s is, %s and has, %s!\n", age, name, sex, spec);
      



    if (spec== s)  

^^^^^^^^ PROBLEM (need s to be the actual letter s. A file is read, and if a certain part of the file (the variable spec) is the letter s then k needs to be increased. Currently this does not seem to work, and I don't know how to make C understand i want it to equal the letter s. I have tried using single and double quotes, and still recieve the error:

"Test.c", line 97: warning: improper pointer/integer combination: op "=="


    { 
       k++;
    } 
  }


  printf("%d", k);
  fclose(myfile);
  return 0;

}

Can anyone help???
Last edited by dsmith : 26-Aug-2004 at 06:55. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 26-Aug-2004, 00:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by PhoenixG
Hi

I'm trying to make a variable in my program equal a letter, i don't know how to do it, i'm sturggling to find out how you can do it, hence the posting here.

Here is the code, bare in mind its still a bit scrappy:

#include <stdio.h>
int main(int argc, char **argv)
. . .
if (spec== s)

^^^^^^^^ PROBLEM (need s to be the actual letter s. A file is read, and if a certain part of the file (the variable spec) is the letter s then k needs to be increased. Currently this does not seem to work, and I don't know how to make C understand i want it to equal the letter s. I have tried using single and double quotes, and still recieve the error:

"Test.c", line 97: warning: improper pointer/integer combination: op "=="


{
k++;
}
}


printf("%d", k);
fclose(myfile);
return 0;

}


Can anyone help???

Hi Phoenix. Yep, many people can help.

The immediate answer to your question is:
CPP / C++ / C Code:
   if (spec== 's')  // put single quotes around a single character.

Now that that's out of the way, you have a few problems I hope you don't mind me pointing out.

1) Use code tags, not font tags around your code.
2) Your definitions:
CPP / C++ / C Code:
char name[10];
char sex[1];
char spec[1];
sex and spec are single characters so there is no need to make them an array. I assume you think they need to be a string, but that would require two characters -- one for the letter and the ending null. I recommend you use simply
CPP / C++ / C Code:
char name[10];
char sex;
char spec;

3) You immediately try to open a file using argv[1], then you check if argv[1] is a valid value. This can cause lots of trouble, not to mention it's bad form. And you are also checking to see if the argv[1] is NULL. Try this instead:
CPP / C++ / C Code:
FILE *myfile;

    if (argc < 2)    // check for not enough parameters
    {
        printf("No command line arguements were found.\n");
        printf("To gain access, enter the name of the text file to open.\n");
        return 0;
    }
    else if (argc > 2)    // check for too many parameters
    {
        printf("Too many arguements entered.\n");
        printf("Please only enter one arguement.\n");
        return 0;
    }
    else 
    {
        myfile = fopen(argv[1], "r");
        if (myfile == NULL)
        {
            printf("Cannot open %s.\n", argv[1]);
            return 0;
        }
    } 

All your argument and file open error conditions have now been tested, you don't need the next IF

4) All your fgetc() stuff is just reading from the file throwing away possibly good data. Simply read the data now, remembering we changed the values above to just chars:
CPP / C++ / C Code:
fscanf(myfile, "%d%s%c%c", &age, name, &sex, &spec);
printf("%d %s is, %c and has, %c!\n", age, name, sex, spec);
And since you are reading a string for name, you need to tell the program where to stop. And fscanf() doesn't handle this well. So make another character array to hold one full line of your file, say buf[] and use:
CPP / C++ / C Code:
fgets(buf, 20, myfile);
sscanf(buf,"%d%9s%c%c",&i, n, &n1, &n2);
printf(" %d) <%s>  %02X/%c  %02X/%c \n", age, name, sex, sex, spec, spec);


The 20 tells fgets() to read up to the end of line or 19 characters, whichever comes first, the places a null in the last char.

the %9s tells sscanf() the function to read 9 characters (all your name string can hold) and fill the last character with the needed null
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get value for variable name defined as string mirizar C Programming Language 3 21-Jun-2004 17:36
Keeping track of a private variable outside the class viperv80 C++ Forum 1 03-Dec-2003 17:25
Help making a survey in C++ elavalos C++ Forum 2 23-Sep-2003 16:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.