Jump to content

Need help with addition in C


darkwolf

Recommended Posts

I have plan to start college this fall but I wanted to get a jump on some of what I will be studying and I have run into some problems. I have a very basic understanding of programming and I have a book I am reading but it's moving too slow and it's so dry I decided to try to do a few things that aren't in the book. This is one of my failed attempts at a simple program. Anyone with any schooling in C should be able to see what I'm doing. I also have included, below the code, the output from gcc tellng me my problems. I just hope someone can help me with this.

#include <stdio.h>;

int num1 = "0";
int num2 = "0";
int answer = "0";

int main(){

	printf("Enter first number to be added");
	num1 = scanf("%s");
	printf("Enter second number to be added");
	num2 = scanf("%s");

	answer = num1 + num2;	
}

return = 0;

adder.c:8: warning: initialization makes integer from pointer without a cast
adder.c:9: warning: initialization makes integer from pointer without a cast
adder.c:10: warning: initialization makes integer from pointer without a cast
adder.c: In function ‘main’:
adder.c:15: warning: too few arguments for format
adder.c:17: warning: too few arguments for format
adder.c:20: warning: control reaches end of non-void function
adder.c: At top level:
adder.c:22: error: syntax error before ‘return’

Thanks everyone for your help.

Link to comment
Share on other sites

int num1 = "0";
int num2 = "0";
int answer = "0";

this should be

int num1 = 0;
int num2 = 0;
int answer = 0;

otherwise your trying to assign a string value to a int.

Secondly, your return should be withing the {} of main. and should not equal 0, but return 0.

return(0);
}

Link to comment
Share on other sites

int num1 = "0";
int num2 = "0";
int answer = "0";

this should be

int num1 = 0;
int num2 = 0;
int answer = 0;

otherwise your trying to assign a string value to a int.

Secondly, your return should be withing the {} of main. and should not equal 0, but return 0.

return(0);
}

Thanks for helping so quickly. I thought that the return looked funny (i used to be capable of getting a few simply programs working about a year ago) but I couldn't pinpoint the problem. The other suggestions helped some also but I'm still getting the errors on lines 15 and 17 saying "too few arguments for format". What do you mean assigning a string value?

I have thought about just looking on the net for source code for a calculator program (which is what I want to eventually turn this into) but didn't like the idea because it's too easy to just copy and paste and not learn how to do the problems. :)

Again thanks for the help you gave thus far.

Link to comment
Share on other sites

here is how I would have done it.

//Simple 2 number adding prog
#include <stdio.h>

int main{

  int num1, num2, answer;                         //sets integer vars
  printf("Enter first number to be added:\n");    //prints text and goes to next line
  scanf("%d", &num1);                             //waits for user to input an integer and sets "num1" to the input
  printf("Enter second number to be added:\n");   //prints text and goes to next line
  scanf("%d", &num2);                             //waits for user to input an integer and sets "num2" to the input
  answer = num1 + num2;                           //performs calculations
  printf("%d + %d = %d\n", num1, num2, answer);   //prints the products and their sum and goes to next line
  return 0;
}

notice how "return 0;" is inside the "main" function

Link to comment
Share on other sites

H@L0_F00, I tried what you suggested and ended up with more errors than what I started with. :( Here is a copy of the errors gcc reported to me.

adder.c:5: error: syntax error before ‘{’ token
adder.c:8: error: syntax error before string constant
adder.c:8: warning: type defaults to ‘int’ in declaration of ‘printf’
adder.c:8: error: conflicting types for ‘printf’
adder.c:8: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
adder.c:8: warning: data definition has no type or storage class
adder.c:9: error: syntax error before string constant
adder.c:9: warning: type defaults to ‘int’ in declaration of ‘scanf’
adder.c:9: error: conflicting types for ‘scanf’
adder.c:9: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
adder.c:9: warning: data definition has no type or storage class
adder.c:10: error: syntax error before string constant
adder.c:10: warning: type defaults to ‘int’ in declaration of ‘printf’
adder.c:10: error: conflicting types for ‘printf’
adder.c:10: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
adder.c:10: warning: data definition has no type or storage class
adder.c:11: error: syntax error before string constant
adder.c:11: warning: type defaults to ‘int’ in declaration of ‘scanf’
adder.c:11: error: conflicting types for ‘scanf’
adder.c:11: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
adder.c:11: warning: data definition has no type or storage class
adder.c:12: warning: type defaults to ‘int’ in declaration of ‘answer’
adder.c:12: error: ‘num1’ undeclared here (not in a function)
adder.c:12: error: ‘num2’ undeclared here (not in a function)
adder.c:12: warning: data definition has no type or storage class
adder.c:13: error: syntax error before string constant
adder.c:13: warning: type defaults to ‘int’ in declaration of ‘printf’
adder.c:13: error: conflicting types for ‘printf’
adder.c:13: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
adder.c:13: warning: data definition has no type or storage class

To ensure that I didn't accidentally cause a problem due to my own negligence I copied and pasted a few times and recompiled every time.

Not that this should matter but for sake of reference I am running OS X 10.5 with gcc 4.0.1.

Link to comment
Share on other sites

i cant believe I didn't notice that little issue lol. it's working fine now thanks. It's just a matter of time now before I have to post again with more problems as I try to develop this to a more functional calculator. Hopefully I'll manage more than I did up till now before I have to bother anyone again. :)

Link to comment
Share on other sites

i cant believe I didn't notice that little issue lol. it's working fine now thanks. It's just a matter of time now before I have to post again with more problems as I try to develop this to a more functional calculator. Hopefully I'll manage more than I did up till now before I have to bother anyone again. :)

did you understand my source? any questions about how any of it works?

Link to comment
Share on other sites

OK, this is all about strong-typing.

Some languages allow you to define scalar variables (may contain any data type). For example, in VBScript you might write:

Dim myVariable
myVariable = 1
myVariable = 1.5
myVariable = "hello"

Notice how it first contained an integer (whole number), then a decimal, then a text string - this is called weak typing.

A language implementing strong-typing forces you to explicitly define what data type a variable is. For example:

int number_one;
number_one = 1;

In this example, I have declared a variable called number_one which can only contain whole numbers (int). If I were to add a third line as follows, it would throw an error telling me I tried to assign the wrong type to my variable:

int number_one;
number_one = 1;
number_one = "1";

There are two kinds of methods you will encounter, those which return a value, and those which do not (voids).

I might want to write a method to get a value, for example:

int getSum(pIntOne, pIntTwo)
{
    return (pIntOne + pIntTwo);
}

I pass in two parameters (which I start with a lower 'p'), I then return an integer. This was defined when I wrote int getSum. Had I tried to do the following, the program would error:

int number_one = 1;
int number_two = 2;
int sum = getSum(number_one, number_two);

string getSum(pIntOne, pIntTwo)
{
    return (pIntOne + pIntTwo);
}

The other kind of method, is one which does not return a value. This is called a void method. It is simply called to perform a task. For example, one for printing error messages to the screen:

void doMessage(pStrMessage, pIntImportance)
{
    switch (pIntImportance)
    {
        case 1:
        {
            print ("Error: " + pStrMessage);
            break;
        }
        case 2:
        {
            print ("Warn: " + pStrMessage);
            break;
        }
        case 3:
        {
            print ("Info: " + pStrMessage);
            break;
        }
        default:
        {
            print ("Debug: " + pStrMessage);
            break;
        }
    }
}

I'm bored now, but I hope you find that useful. To learn how to convert between data types, i.e. how to turn your decimal into an integer, read this page about casting. I'm not a c programmer, but it seems odd to me you defined "int main()", why should main return an integer? How about "void main()"?

Link to comment
Share on other sites

Ok I'm confused about the casting but I will look into that and do plenty more studying. Obviously I am not ready to write anything of much use at the moment since I had issue with such a simple program lol. I cant wait for summer so I can start classes. Then I will be able to do a little more. :)

Thanks everyone.

Link to comment
Share on other sites

Ok I'm confused about the casting but I will look into that and do plenty more studying. Obviously I am not ready to write anything of much use at the moment since I had issue with such a simple program lol. I cant wait for summer so I can start classes. Then I will be able to do a little more. :)

Thanks everyone.

Casting is really easy.

All you need do is place the desired type in parentheses (fancy word for brackets) and follow it with the variable.

Say you had a char, 'numb' containing the value '1' that you wanted to perform mathematical work on, you only need do (int)numb to cast the char to an int.

It's useful and worth grasping.

Link to comment
Share on other sites

OK, this is all about strong-typing.

Some languages allow you to define scalar variables (may contain any data type). For example, in VBScript you might write:

Dim myVariable
myVariable = 1
myVariable = 1.5
myVariable = "hello"

Notice how it first contained an integer (whole number), then a decimal, then a text string - this is called weak typing.

A language implementing strong-typing forces you to explicitly define what data type a variable is. For example:

int number_one;
number_one = 1;

In this example, I have declared a variable called number_one which can only contain whole numbers (int). If I were to add a third line as follows, it would throw an error telling me I tried to assign the wrong type to my variable:

int number_one;
number_one = 1;
number_one = "1";

There are two kinds of methods you will encounter, those which return a value, and those which do not (voids).

I might want to write a method to get a value, for example:

int getSum(pIntOne, pIntTwo)
{
    return (pIntOne + pIntTwo);
}

I pass in two parameters (which I start with a lower 'p'), I then return an integer. This was defined when I wrote int getSum. Had I tried to do the following, the program would error:

int number_one = 1;
int number_two = 2;
int sum = getSum(number_one, number_two);

string getSum(pIntOne, pIntTwo)
{
    return (pIntOne + pIntTwo);
}

The other kind of method, is one which does not return a value. This is called a void method. It is simply called to perform a task. For example, one for printing error messages to the screen:

void doMessage(pStrMessage, pIntImportance)
{
    switch (pIntImportance)
    {
        case 1:
        {
            print ("Error: " + pStrMessage);
            break;
        }
        case 2:
        {
            print ("Warn: " + pStrMessage);
            break;
        }
        case 3:
        {
            print ("Info: " + pStrMessage);
            break;
        }
        default:
        {
            print ("Debug: " + pStrMessage);
            break;
        }
    }
}

I'm bored now, but I hope you find that useful. To learn how to convert between data types, i.e. how to turn your decimal into an integer, read this page about casting. I'm not a c programmer, but it seems odd to me you defined "int main()", why should main return an integer? How about "void main()"?

The reason you declare main as returning an int is that all programs return status codes to the operating system - especially in Unix/Linux. By convention, returning zero means everything went ok.

I generally recommend avoiding any calls to scanf() directly. Instead, read the string in and perform sscanf() on the string. scanf() skips whitespace in ways that can be confusing.

-Hak Ron

Link to comment
Share on other sites

The reason you declare main as returning an int is that all programs return status codes to the operating system - especially in Unix/Linux. By convention, returning zero means everything went ok.

Interesting. I clearly remember Java lessons of "private static void main (final String[] pArgs)". I guess that's something I need to read up on.

Link to comment
Share on other sites

The reason you declare main as returning an int is that all programs return status codes to the operating system - especially in Unix/Linux. By convention, returning zero means everything went ok.

I generally recommend avoiding any calls to scanf() directly. Instead, read the string in and perform sscanf() on the string. scanf() skips whitespace in ways that can be confusing.

-Hak Ron

"main" doesn't have to be declared as "int" but it is a good practice to do so

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...