Thread: Error that I can't figure out.

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Error that I can't figure out.

    Hello, I am writing a program for one of my classes and I've worked out all of the errors except one. I've spent the last hour trying to figure out what I've done wrong but I still can't. Any help would be greatly appreciated =)

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    float carbon_amu=12.011;
    float hydrogen_amu=1.0079;
    float oxygen_amu=15.9994;
    float avogadros_number=6.02e23;
    
    
    int number_of_carbon_atoms=0;
    int number_of_hydrogen_atoms=0;
    int number_of_oxygen_atoms=0;
    
    float number_of_grams_of_molecule=0;
    
    
    char molecule_name[80];
    
    
       printf("What is the name of your CHO molecule? \n");
       scanf("%s", &molecule_name);
       
       printf("How many carbon atoms are in %s? \n", molecule_name);
       scanf("%d", &number_of_carbon_atoms);
       
       printf("How many hydrogen atoms are in %s? \n", molecule_name);
       scanf("%d", &number_of_hydrogen_atoms);
       
       printf("How many oxygen atoms are in %s? \n", molecule_name);
       scanf("%d", &number_of_oxygen_atoms);
       
       printf("How many grams of %s do you have?\n", molecule_name);
       scanf("%f", &number_of_grams_of_molecule);
       
       
       float carbon_mass_per_mole=number_of_carbon_atoms*carbon_amu;
       float hydrogen_mass_per_mole=number_of_hydrogen_atoms*hydrogen_amu;
       float oxygen_mass_per_mole=number_of_oxygen_atoms*oxygen_amu;
       float molecule_mass_per_mole=carbon_mass_per_mole+hydrogen_mass_per_mole+oxygen_mass_per_mole;
       float number_of_molecules=number_of_grams_of_molecule/molecule_mass_per_mole*avogadros_number;
       
       
       printf(" \n");
       printf("Results:\n");
       printf("--------\n");
       printf("The mass of carbon in %f is:\n", carbon_mass_per_mole);
       printf("The mass of hydrogen in %f is:\n", hydrogen_mass_per_mole);
       printf("The mass of oxygen in %f is:\n", oxygen_mass_per_mole);
       
       
       printf("--------------------------------------------------------\n");
       printf("The number of %s molecules in %f grams is %f\n", molecule_name, number_of_grams_of_molecule, number_of_molecules);
       
    }
    The error that I have been getting is:

    a03.c: In function 'main':
    a03.c:25:4: warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[80]'


    Thanks again in advance!

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Also, I deleted the comment that I posted at the beginning of the program so the line that the error is occurring on should actually be 23. Sorry for any confusion.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char molecule_name[80];
    printf("What is the name of your CHO molecule? \n");
    scanf("%s", &molecule_name);
    Lose the &. The name of an array when passed to a function is a pointer to the type of its first element. So it's passed as a pointer to character. See also: Arrays and Pointers


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Perfect, thanks a lot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1 Error I Cant Figure Out!! Tried All I Could Think Of!!
    By irishfeeney92 in forum C Programming
    Replies: 16
    Last Post: 04-30-2011, 04:17 PM
  2. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  3. I cant figure out how to fix this error
    By Raigne in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 02:04 AM
  4. can't figure this error out
    By the Wookie in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2003, 03:23 PM
  5. getting an error I can't figure out....
    By Soopafly in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2002, 09:30 AM