I am getting all sorts of incorrect type warnings when I compile the following code:

// hw1.c
// Josh Soileau
// 1/19/10
// Program: Asks / stores info on a bank
//

#include<stdio.h>

int main(){
        char name[25] = "blank", rating[25] = "blank", state[25] = "blank";
        int lobbyists=0, prevemploys=0, number=0;
        double networth=0, tarp=0, contributions=0;

        printf("\n\n(1)Bank name: %s\n(2)State: %d\n(3)Net worth: %d\n", name, state, networth);
        printf("(4)Rating: %s\n(5)TARP money: %d\n(6)Campaign contributions: %d\n", rating, tarp, contributions);
        printf("(7)Lobbyists in capitol: %d\n(8)Previous Employees in government: %d\n", lobbyists, prevemploys);
        printf("\n(9)Display Data\n(10)Clear all Data\n(11)Quit\n\n");

while (number != 11){
        printf("Type the number to edit the field / perform task, and press ENTER: \n\n");
        scanf("%d",&number);
        switch (number) {
                case 1:
                        printf("Enter the name of the bank: ");
                        scanf("%s",&name);
                        printf("%s\n\n",name);
                case 2:
                        printf("Enter the name of the state in which this bank is located: ");
                        scanf("%s",&state);
                        printf("%s\n\n",state);
                case 3:
                        printf("Enter the bank's net worth: ");
                        scanf("%d",&networth);
                        printf("%d\n\n",networth);
                case 4:
                        printf("Enter the bank's rating: ");
                        scanf("%s",&rating);
                        printf("%s\n\n", rating);
                default:
                        printf("Please choose one of the desired options' numbers only.");
                }
}
        return 0;
}

Please keep in mind that this is by no means a finished program. I had to stop coding because the warnings where too much to look at every time I compiled it.

Here is everything I get when I compile:
hw1.c: In function âmainâ:
hw1.c:14: warning: format â%dâ expects type âintâ, but argument 3 has type âchar *â
hw1.c:14: warning: format â%dâ expects type âintâ, but argument 4 has type âdoubleâ
hw1.c:15: warning: format â%dâ expects type âintâ, but argument 3 has type âdoubleâ
hw1.c:15: warning: format â%dâ expects type âintâ, but argument 4 has type âdoubleâ
hw1.c:25: warning: format â%sâ expects type âchar *â, but argument 2 has type âchar (*)[25]â
hw1.c:29: warning: format â%sâ expects type âchar *â, but argument 2 has type âchar (*)[25]â
hw1.c:33: warning: format â%dâ expects type âint *â, but argument 2 has type âdouble *â
hw1.c:34: warning: format â%dâ expects type âintâ, but argument 2 has type âdoubleâ
hw1.c:37: warning: format â%sâ expects type âchar *â, but argument 2 has type âchar (*)[25]â

Also, the reason for my while loop is that I need the code to continue asking for "number" after every case in my switch statement. That way, the user can keep changing the different variables as they wish. I need the program to only terminate if number is equal to 11.

There has got to be a more elegant way of going about this. I thought about putting "continue" at the end of each switch case so it resets, but I don't think that will work.

Any ideas?

Thank you all!
Soileau

WaltP commented: Now that you've cleaned up the warnings for him, what has he learned? -2
Ezzaral commented: Rep correction. The neg rep was meant for a follow-up post, not this one. +10

This cleans up the warnings/errors

#include<stdio.h>

int main(){
        char name[25] = "blank", rating[25] = "blank", state[25] = "blank";
        int lobbyists=0, prevemploys=0, number=0;
        double networth=0, tarp=0, contributions=0;

        printf("\n\n(1)Bank name: %s\n(2)State: %s\n(3)Net worth: %f\n", name, state, networth);
        printf("(4)Rating: %s\n(5)TARP money: %f\n(6)Campaign contributions: %f\n", rating, tarp, contributions);
        printf("(7)Lobbyists in capitol: %d\n(8)Previous Employees in government: %d\n", lobbyists, prevemploys);
        printf("\n(9)Display Data\n(10)Clear all Data\n(11)Quit\n\n");

while (number != 11){
        printf("Type the number to edit the field / perform task, and press ENTER: \n\n");
        scanf("%d",&number);
        switch (number) {
                case 1:
                        printf("Enter the name of the bank: ");
                        scanf("%s",name);
                        printf("%s\n\n",name);
                case 2:
                        printf("Enter the name of the state in which this bank is located: ");
                        scanf("%s",state);
                        printf("%s\n\n",state);
                case 3:
                        printf("Enter the bank's net worth: ");
                        scanf( "%lf", &networth);
                        printf("%f\n\n",networth);
                case 4:
                        printf("Enter the bank's rating: ");
                        scanf("%s",rating);
                        printf("%s\n\n", rating);
                default:
                        printf("Please choose one of the desired options' numbers only.");
                }
}
        return 0;
}

You really should get a good book on C

commented: Now that you've cleaned up the warnings for him, what has he learned? -2

Thanks a bunch for those helping me with those warnings.

This is my first real programming class, so I am not exactly wonderful at it yet. But it'll come with practice.

Anyone have any clue with my program logic problem?

Putting a break at the end of each case statement should work.

case 4:      
                  printf("Enter the bank's rating: ");      
                  scanf("%s",rating);             
                  printf("%s\n\n", rating);
                  break;

Oh No!!! Not the String/scanf() faux pax!!!!

scanf("%s",name);
                        scanf("%s",state);
                        scanf("%s",rating);

Please read this

Also, notice that your case function looks like this :

switch(number)
{
  case 1: //blah
  case 2: //blah
}

There is a problem with that. The problem is that you used constant
numbers in your switch case. Usually, those numbers means something,
so making a const variable for those number, would make your code
better. Its better to get into a good habit early.
So for example, you can do something like this :

const int QUIT = -1;
const int GET_BANK_NAME = 1;
const int GET_STATE_NAME = 2;
const int PRINT_NET_WORTH = 3;

while( input != QUIT )
{
    //some stuff goes here

   switch( input )
  {
    case GET_BANK_NAME : //logic here
    case GET_STATE_NAME : //logic here
    case PRINT_NET_WORTH : //logic here
    case QUIT : //logic here
   }
}

See how much more readable and better it looks?

Also, notice that your case function looks like this :

switch(number)
{
  case 1: //blah
  case 2: //blah
}

There is a problem with that. The problem is that you used constant
numbers in your switch case. Usually, those numbers means something,
so making a const variable for those number, would make your code
better. Its better to get into a good habit early.
So for example, you can do something like this :

const int QUIT = -1;
const int GET_BANK_NAME = 1;
const int GET_STATE_NAME = 2;
const int PRINT_NET_WORTH = 3;

See how much more readable and better it looks?

But it will never compile. Did you forget this was the C forum? :icon_wink:

#define QUIT = -1;
#define GET_BANK_NAME = 1;
#define GET_STATE_NAME = 2;
#define PRINT_NET_WORTH = 3;

is C. There is no problem with using constant (called hardcoded) numbers in a switch statement. You are correct though that constants are a good habit to get into... :icon_wink:

Anyway this suggestion does nothing to fix your logic problem. And since you didn't tell us what happens that makes you think there is a logic problem, what can we tell you? No info = no ideas generally

Hmmm. Well, my logic works, I suppose. I have my switch statement in a while loop, so that it will repeat itself until option 11 is chosen. This works, but it seems like there would be a better way of repeating the switch statement until one particular case is chosen. I was wondering if there was another way I had not thought of.

Okay, I have written more code to the program, and I am now getting a few more error messages. Here is the new code which is giving me problems:

case 10:
                        name[25] = "unknown";
                        state[25] = "unkown";
                        networth = 0;
                        rating[25] = "unknown";
                        tarp = 0;
                        contributions = 0;
                        lobbyists = 0;
                        prevemploys = 0;

                        printf("\n\nAll data cleared. Enter 9 for more options.\n\n");

                        break;

I am getting these errors:

hw1.c: In function âmainâ:
hw1.c:79: warning: assignment makes integer from pointer without a cast
hw1.c:80: warning: assignment makes integer from pointer without a cast
hw1.c:82: warning: assignment makes integer from pointer without a cast

These are the lines where I try to reset my character variables to "unknown". I don't know why I am getting them.

Again, I apologize if these warnings are blatantly obvious to you, I have only recently started programming.

it will never compile. Did you forget this was the C forum?

ya I did. Lately, I been of my game. My school started so I have no time
for leisure and I have a lot of stress right now. Sorry about that.

Because in C you can't assign a string to a single character: name[25] = "unknown"; state[25] = "unkown"; You need to look into the function strcpy() .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.