0

What I am trying to do is to have the user input their information. Like state for example. I need to process this state abbreviation and output it as capital letters. I'm confused at how to do that because I am using structures. When I use what I am using below it tells me they are incompatible and it doesn't work. What should I do differently. I've tried pretty much everything. This is in C.

for (i = 0; i < 3 != '\0'; i++) {
    people[i].state = toupper(people[i].state);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Drew
  • 23
  • 1
  • 10
    `i < 3 != '\0'` what is this? – Sourav Ghosh Feb 21 '17 at 18:13
  • 1
    `toupper()` works on one character at a time. It's hard to be sure because you haven't given the definition of your structure, but it looks like you're trying to apply it to a whole string. – John Bollinger Feb 21 '17 at 18:18
  • 5
    @SouravGhosh The fanciest way to check if `i` is less than `3` I've seen so far. – Eugene Sh. Feb 21 '17 at 18:20
  • @EugeneSh. True, but I still suspect that wasn't the intent of that code. Ouch. – Andrew Henle Feb 21 '17 at 18:22
  • For future questions, not that *Using `toupper()` with a structure* has no reason to be something special, `toupper()` takes a character and returns the *upper*case of it and that's all. It doesn't care where did the character come from. – Iharob Al Asimi Feb 21 '17 at 18:33

1 Answers1

4

It seems you mean the following

for ( i = 0; i < 3; i++ )
{
    for ( char *p = people[i].state; *p; ++p ) *p = toupper( ( unsigned char )*p );
}

Or if you have a single object of the structure type then something like

for ( i = 0; i < 3; i++ )
{
    people.state[i] = toupper( ( unsigned char )people.state[i] );
}

or even

for ( i = 0; i < 3 && people.state[i] != '\0'; i++ )
{
    people.state[i] = toupper( ( unsigned char )people.state[i] );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    @J.Piquard It is not standard C function. – Vlad from Moscow Feb 21 '17 at 18:26
  • @J.Piquard Ref: [strupr() and strlwr() in string.h part are of the ANSI standard?](http://stackoverflow.com/q/26327812/2410359) – chux - Reinstate Monica Feb 21 '17 at 21:11
  • When I put any of these in it gives me an error that says [Error]: incompatible types in assignment – Drew Feb 22 '17 at 02:18
  • @Drew What are the types of people and state? – Vlad from Moscow Feb 22 '17 at 08:46
  • @vladfromMoscow They are a type of character in a structure. I need to output it as two capital letters.
     
        #include 
        #include 
    
         struct info
         {
         char full_name[35];
         char address [50];
         char city [25];
         char state [3];
         long int zip_code;
         int age;
         }
    
        int main()
        {
     
         for (i = 0; i < 3; ++i) 
           {
             people[i].state = toupper(people[i].state);
          }
    – Drew Feb 23 '17 at 22:51