Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,533 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,358 people online right now. Registration is fast and FREE... Join Now!




Declaring variables

 
Reply to this topicStart new topic

Declaring variables, Disabled person trying to figure out C and it won't work!

revron
17 Sep, 2007 - 12:03 PM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 13


My Contributions
CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initial cost, electric bill, water and sewer, tax rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water and sewer);
        printf("\n How much is your tax rate? ");
        scanf("%d", &tax rate);
        


    initial cost =  electric bill * water and sewer * taxrate;
    total = initial cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial cost);
    printf ("\n Electric Bill = %d", electric bill);
    printf ("\n Water And Sewer = %d" , water and sewer);
    printf ("\n Tax Rate = %3.8" , tax rate);
    printf ("\n Total = %15d" , total);
    return (0);
}


MOD EDIT: modified title ~ jayman9

User is offlineProfile CardPM
+Quote Post


no2pencil
RE: Declaring Variables
17 Sep, 2007 - 12:05 PM
Post #2

Unix Ronin
Group Icon

Joined: 10 May, 2007
Posts: 10,438



Thanked: 198 times
Dream Kudos: 2725
Expert In: Goofing Off

My Contributions
What is the error(s) that you are getting?

Is the error in processing or compiling?
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Declaring Variables
17 Sep, 2007 - 12:08 PM
Post #3

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
I'm a beginner programmer as well, but don't the variable names have to be one string? So instead of variable name being "water and sewer", shouldn't it be "water_and_sewer" or something like that?
User is offlineProfile CardPM
+Quote Post

revron
RE: Declaring Variables
17 Sep, 2007 - 12:09 PM
Post #4

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 13


My Contributions
QUOTE(no2pencil @ 17 Sep, 2007 - 01:05 PM) *

What is the error(s) that you are getting?

Is the error in processing or compiling?



When I try to compile it.

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Declaring Variables
17 Sep, 2007 - 12:10 PM
Post #5

Unix Ronin
Group Icon

Joined: 10 May, 2007
Posts: 10,438



Thanked: 198 times
Dream Kudos: 2725
Expert In: Goofing Off

My Contributions
QUOTE(ShotokanDeity @ 17 Sep, 2007 - 01:08 PM) *

I'm a beginner programmer as well, but don't the variable names have to be one string? So instead of variable name being "water and sewer", shouldn't it be "water_and_sewer" or something like that?

Correct, you cannot have spaces in a variable name.

CODE

int initial cost, electric bill, water and sewer, tax rate; total;

CODE

int initial_cost;
int electric_bill;
int water_and_sewer;
int tax_rate;
int total;




QUOTE(revron @ 17 Sep, 2007 - 01:09 PM) *

QUOTE(no2pencil @ 17 Sep, 2007 - 01:05 PM) *

What is the error(s) that you are getting?

Is the error in processing or compiling?



When I try to compile it.

& What is that error? Can you copy & paste it here?
User is offlineProfile CardPM
+Quote Post

revron
RE: Declaring Variables
17 Sep, 2007 - 12:59 PM
Post #6

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 13


My Contributions
QUOTE(no2pencil @ 17 Sep, 2007 - 01:10 PM) *

QUOTE(ShotokanDeity @ 17 Sep, 2007 - 01:08 PM) *

I'm a beginner programmer as well, but don't the variable names have to be one string? So instead of variable name being "water and sewer", shouldn't it be "water_and_sewer" or something like that?

Correct, you cannot have spaces in a variable name.

CODE

int initial cost, electric bill, water and sewer, tax rate; total;

CODE

int initial_cost;
int electric_bill;
int water_and_sewer;
int tax_rate;
int total;




QUOTE(revron @ 17 Sep, 2007 - 01:09 PM) *

QUOTE(no2pencil @ 17 Sep, 2007 - 01:05 PM) *

What is the error(s) that you are getting?

Is the error in processing or compiling?



When I try to compile it.

& What is that error? Can you copy & paste it here?



Ok, I fixed everything with underscores between the words. But now I get

multiple definition of _main error
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Declaring Variables
17 Sep, 2007 - 01:32 PM
Post #7

Alphanumeric
Group Icon

Joined: 18 Feb, 2007
Posts: 4,297



Thanked: 190 times
Dream Kudos: 1050
Expert In: Java/C++

My Contributions
did you remember to change all the the occurrences?
CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main()
{
    int initial_cost, electric_bill, water_and sewer, tax_rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial_cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric_bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water_and_sewer);
    printf("\n How much is your tax rate? ");
    scanf("%d", &tax_rate);
    
    initial_cost =  electric_bill * water_and_sewer * taxrate;
    total = initial_cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial_cost);
    printf ("\n Electric Bill = %d", electric_bill);
    printf ("\n Water And Sewer = %d" , water_and_sewer);
    printf ("\n Tax Rate = %3.8" , tax_rate);
    printf ("\n Total = %15d" , total);
    return (0);
}
If so, can you repost your code?
User is online!Profile CardPM
+Quote Post

Bench
RE: Declaring Variables
17 Sep, 2007 - 01:57 PM
Post #8

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(revron @ 17 Sep, 2007 - 09:59 PM) *

Ok, I fixed everything with underscores between the words. But now I get

multiple definition of _main error

What IDE are you using to compile your code? (I assume you're using an IDE?) It sounds to me as if you've got multiple source files in one project and its feeding the compiler several different main() functions from your different source files.

User is offlineProfile CardPM
+Quote Post

revron
RE: Declaring Variables
17 Sep, 2007 - 02:36 PM
Post #9

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 13


My Contributions
QUOTE(NickDMax @ 17 Sep, 2007 - 02:32 PM) *

did you remember to change all the the occurrences?
CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main()
{
    int initial_cost, electric_bill, water_and sewer, tax_rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial_cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric_bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water_and_sewer);
    printf("\n How much is your tax rate? ");
    scanf("%d", &tax_rate);
    
    initial_cost =  electric_bill * water_and_sewer * taxrate;
    total = initial_cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial_cost);
    printf ("\n Electric Bill = %d", electric_bill);
    printf ("\n Water And Sewer = %d" , water_and_sewer);
    printf ("\n Tax Rate = %3.8" , tax_rate);
    printf ("\n Total = %15d" , total);
    return (0);
}
If so, can you repost your code?



QUOTE(revron @ 17 Sep, 2007 - 03:32 PM) *

QUOTE(NickDMax @ 17 Sep, 2007 - 02:32 PM) *

did you remember to change all the the occurrences?
CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main()
{
    int initial_cost, electric_bill, water_and sewer, tax_rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial_cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric_bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water_and_sewer);
    printf("\n How much is your tax rate? ");
    scanf("%d", &tax_rate);
    
    initial_cost =  electric_bill * water_and_sewer * taxrate;
    total = initial_cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial_cost);
    printf ("\n Electric Bill = %d", electric_bill);
    printf ("\n Water And Sewer = %d" , water_and_sewer);
    printf ("\n Tax Rate = %3.8" , tax_rate);
    printf ("\n Total = %15d" , total);
    return (0);
}
If so, can you repost your code?




Ok, I changed all the occurances and now I get
an error that says undeclared indentifier for electric_bill water_and_sewer tax_rate and total. It also says statement has no effect.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Declaring Variables
17 Sep, 2007 - 02:47 PM
Post #10

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Can you copy and past the exact error messages that you are receiving, along with your most recent code?
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Declaring Variables
18 Sep, 2007 - 01:49 AM
Post #11

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,353



Thanked: 81 times
Dream Kudos: 2800
Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
CODE
total;

You need to define the variable's data type. double total; would be appropriate.

CODE
initial_cost =  electric_bill * water_and_sewer * taxrate;

Typo. taxrate should be tax_rate.
User is offlineProfile CardPM
+Quote Post

vijaysnkar
RE: Declaring Variables
18 Sep, 2007 - 02:18 AM
Post #12

D.I.C Head
**

Joined: 11 Aug, 2007
Posts: 55



Thanked: 1 times
My Contributions
Here is the error free code

CODE


#include <stdio.h>
#include <stdlib.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initialcost, electricbill, waterandsewer, taxrate,total;

    printf("\n How much is your house? ");
    scanf("%d", &initialcost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electricbill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &waterandsewer);
    printf("\n How much is your tax rate? ");
    scanf("%d", &taxrate);

    initialcost =  electricbill * waterandsewer * taxrate;
    total = initialcost * HOUSECOST;
    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initialcost);
    printf ("\n Electric Bill = %d", electricbill);
    printf ("\n Water And Sewer = %d" , waterandsewer);
    printf ("\n Tax Rate = %3.8" , taxrate);
    printf ("\n Total = %15d\n" , total);
    system("pause");
    return (0);
}






QUOTE(revron @ 17 Sep, 2007 - 01:03 PM) *

CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initial cost, electric bill, water and sewer, tax rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water and sewer);
        printf("\n How much is your tax rate? ");
        scanf("%d", &tax rate);
        


    initial cost =  electric bill * water and sewer * taxrate;
    total = initial cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial cost);
    printf ("\n Electric Bill = %d", electric bill);
    printf ("\n Water And Sewer = %d" , water and sewer);
    printf ("\n Tax Rate = %3.8" , tax rate);
    printf ("\n Total = %15d" , total);
    return (0);
}


MOD EDIT: modified title ~ jayman9


User is offlineProfile CardPM
+Quote Post

revron
RE: Declaring Variables
22 Sep, 2007 - 03:35 PM
Post #13

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 13


My Contributions
QUOTE(vijaysnkar @ 18 Sep, 2007 - 03:18 AM) *

Here is the error free code

CODE


#include <stdio.h>
#include <stdlib.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initialcost, electricbill, waterandsewer, taxrate,total;

    printf("\n How much is your house? ");
    scanf("%d", &initialcost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electricbill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &waterandsewer);
    printf("\n How much is your tax rate? ");
    scanf("%d", &taxrate);

    initialcost =  electricbill * waterandsewer * taxrate;
    total = initialcost * HOUSECOST;
    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initialcost);
    printf ("\n Electric Bill = %d", electricbill);
    printf ("\n Water And Sewer = %d" , waterandsewer);
    printf ("\n Tax Rate = %3.8" , taxrate);
    printf ("\n Total = %15d\n" , total);
    system("pause");
    return (0);
}






QUOTE(revron @ 17 Sep, 2007 - 01:03 PM) *

CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initial cost, electric bill, water and sewer, tax rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water and sewer);
        printf("\n How much is your tax rate? ");
        scanf("%d", &tax rate);
        


    initial cost =  electric bill * water and sewer * taxrate;
    total = initial cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial cost);
    printf ("\n Electric Bill = %d", electric bill);
    printf ("\n Water And Sewer = %d" , water and sewer);
    printf ("\n Tax Rate = %3.8" , tax rate);
    printf ("\n Total = %15d" , total);
    return (0);
}


MOD EDIT: modified title ~ jayman9



User is offlineProfile CardPM
+Quote Post

revron
RE: Declaring Variables
22 Sep, 2007 - 03:41 PM
Post #14

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 13


My Contributions
QUOTE(revron @ 22 Sep, 2007 - 04:35 PM) *

QUOTE(vijaysnkar @ 18 Sep, 2007 - 03:18 AM) *

Here is the error free code

CODE


#include <stdio.h>
#include <stdlib.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initialcost, electricbill, waterandsewer, taxrate,total;

    printf("\n How much is your house? ");
    scanf("%d", &initialcost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electricbill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &waterandsewer);
    printf("\n How much is your tax rate? ");
    scanf("%d", &taxrate);

    initialcost =  electricbill * waterandsewer * taxrate;
    total = initialcost * HOUSECOST;
    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initialcost);
    printf ("\n Electric Bill = %d", electricbill);
    printf ("\n Water And Sewer = %d" , waterandsewer);
    printf ("\n Tax Rate = %3.8" , taxrate);
    printf ("\n Total = %15d\n" , total);
    system("pause");
    return (0);
}






QUOTE(revron @ 17 Sep, 2007 - 01:03 PM) *

CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initial cost, electric bill, water and sewer, tax rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water and sewer);
        printf("\n How much is your tax rate? ");
        scanf("%d", &tax rate);
        


    initial cost =  electric bill * water and sewer * taxrate;
    total = initial cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial cost);
    printf ("\n Electric Bill = %d", electric bill);
    printf ("\n Water And Sewer = %d" , water and sewer);
    printf ("\n Tax Rate = %3.8" , tax rate);
    printf ("\n Total = %15d" , total);
    return (0);
}


MOD EDIT: modified title ~ jayman9





Still have errors such as:
undeclared identifier 'cost'
undeclared identifier 'electric'
statement has no effect
syntax error , missing semi colon before 'bill'
undeclared identifier 'bill'
undeclared identifier 'water'
statement has no effect
syntax error , missing semi colon before 'and'
undeclared identifier 'and'
statement has no effect
syntax error , missing semi colon before 'sewer'
undeclared identifier 'sewer'
and so and and so forth with all of the rest of the words
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Declaring Variables
23 Sep, 2007 - 05:54 PM
Post #15

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 7 times
Dream Kudos: 775
Expert In: C,C++

My Contributions

Here is the error free code

CODE


#include <stdio.h>
#include <stdlib.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initialcost, electricbill, waterandsewer, taxrate,total;

    printf("\n How much is your house? ");
    scanf("%d", &initialcost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electricbill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &waterandsewer);
    printf("\n How much is your tax rate? ");
    scanf("%d", &taxrate);

    initialcost =  electricbill * waterandsewer * taxrate;
    total = initialcost * HOUSECOST;
    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initialcost);
    printf ("\n Electric Bill = %d", electricbill);
    printf ("\n Water And Sewer = %d" , waterandsewer);
    printf ("\n Tax Rate = %3.8" , taxrate);
    printf ("\n Total = %15d\n" , total);
    system("pause");
    return (0);
}






[quote name='revron' post='256975' date='17 Sep, 2007 - 01:03 PM']
CODE
#include <stdio.h>
#define HOUSECOST 3.8

/*    This program calculates the cost of a house for 15 years  */

int main(void)
{
    int initial cost, electric bill, water and sewer, tax rate;
    total;

    printf("\n How much is your house? ");
    scanf("%d", &initial cost);
    printf("\n How much is your electric bill? ");
    scanf("%d", &electric bill);
    printf("\n How much is your water and sewer? ");
    scanf("%d", &water and sewer);
        printf("\n How much is your tax rate? ");
        scanf("%d", &tax rate);
        


    initial cost =  electric bill * water and sewer * taxrate;
    total = initial cost * HOUSECOST;

    printf ("\n    Ron A     ");
    printf ("\n Initial Cost = %d", initial cost);
    printf ("\n Electric Bill = %d", electric bill);
    printf ("\n Water And Sewer = %d" , water and sewer);
    printf ("\n Tax Rate = %3.8" , tax rate);
    printf ("\n Total = %15d" , total);
    return (0);
}


MOD EDIT: modified title ~ jayman9
[/quote]


Still have errors such as:
undeclared identifier 'cost'
undeclared identifier 'electric'
statement has no effect
syntax error , missing semi colon before 'bill'
undeclared identifier 'bill'
undeclared identifier 'water'
statement has no effect
syntax error , missing semi colon before 'and'
undeclared identifier 'and'
statement has no effect
syntax error , missing semi colon before 'sewer'
undeclared identifier 'sewer'
and so and and so forth with all of the rest of the words
[/quote]

the corrected code that was posted should compile just fine (this is the first block of code quoted above). the second block of code does not have the corrections in place - there are still whitespaces in the variable names, and the variable total is not declared with a type.

if you copy-paste the first block (the corrected version provided by vijaysnkar) into your file, it will work fine. if you use the second, it won't compile, giving many of the errors you just listed. make sure that you're not creating a new file to hold the corrected code, as this may cause a multiple-definitions error for main().

-jjh
User is offlineProfile CardPM
+Quote Post

revron
RE: Declaring Variables
24 Sep, 2007 - 02:45 PM
Post #16

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 13


My Contributions
I got this code to run! Thank all of you VERY,VERY much!

Ron
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/5/09 12:00PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month