C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2009, 06:45 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 3
Beginner Error

I've just started the C programming tutorial and I'm trying to modify the code to practise what I've learnt. This is what I have:

Code:
#include <stdio.h>

int main()
{
  int num1, num2, ans1;
  printf( "Insert a number pl0x: " );
  scanf("%d", &num1);
  printf("Insert another number pl0x: ", &num2);
  scanf("%d", &num2);
  ans1 = num1 + num2;
  printf("The two numers entered added together are: %d", &ans1);
  getchar();
  return 0;
}
No matter what number I enter, I get the same (wrong response):

Code:
Insert a number pl0x: 3
Insert another number pl0x: 3
The two numers entered added together are: 2293580
I think it has something to do with the last printf statement. Can anyone point me into the right direction?
SvenRL is offline   Reply With Quote
Old 07-03-2009, 06:56 PM   #2
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Quote:
Originally Posted by SvenRL View Post
I think it has something to do with the last printf statement. Can anyone point me into the right direction?
For what it's worth, here's my 2c.
Code:
#include <stdio.h>

int main()
{
  int num1, num2, ans1;
  printf( "Insert a number pl0x: " );
  scanf("%d", &num1);
  printf("Insert another number pl0x: ", &num2);                   /* why do you need &num2 here?? */
  scanf("%d", &num2);
  ans1 = num1 + num2;
  printf("The two numers entered added together are: %d", &ans1);  /* print the contents of ans1 not its location */
  getchar();                                                       /* why do you need to read more stuff?? */
  return 0;
}

Last edited by itCbitC; 07-03-2009 at 06:59 PM.
itCbitC is offline   Reply With Quote
Old 07-05-2009, 03:40 AM   #3
Registered User
 
Join Date: Jul 2009
Posts: 3
Quote:
Originally Posted by itCbitC View Post
For what it's worth, here's my 2c.
Code:
#include <stdio.h>

int main()
{
  int num1, num2, ans1;
  printf( "Insert a number pl0x: " );
  scanf("%d", &num1);
  printf("Insert another number pl0x: ", &num2);                   /* why do you need &num2 here?? */
  scanf("%d", &num2);
  ans1 = num1 + num2;
  printf("The two numers entered added together are: %d", &ans1);  /* print the contents of ans1 not its location */
  getchar();                                                       /* why do you need to read more stuff?? */
  return 0;
}
Thank you for the quick response and constructive criticism. I'm guessing my mistake is putting the num2 into the printf statement. I don't need it there.

so....

What I seem to be doing wrong is the mistake mentioned above and the fact that I'm printing the location of ans1 and not the contents of.

The getchar() at the end of my program is simply to prevent the DOS window from closing. Is that not the write way to do it? It's the only way I know of. Suggestions would be great.
SvenRL is offline   Reply With Quote
Old 07-05-2009, 05:38 AM   #4
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,258
The red highlight was showing you what was wrong. You only use & if you need the address of a variable, rather than the value the variable contains. Typically with printf you don't need an address, but a value.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 07-05-2009, 09:12 AM   #5
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by SvenRL View Post
The getchar() at the end of my program is simply to prevent the DOS window from closing. Is that not the write way to do it? It's the only way I know of. Suggestions would be great.
Here are a list of methods to stop the console from closing.
__________________
HOPE YOU UNDERSTAND.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Old 07-07-2009, 12:02 AM   #6
Registered User
 
Automatik's Avatar
 
Join Date: Jun 2009
Posts: 1
You're getting the wrong value because you're using &(address of operator) which is not needed in the case of using printf()

the getchar() isn't needed either, I assume you're on Windows and running the program by double clicking.. you could remove the getchar() and launch the program from cmd (Start>Run>cmd) then cd to your working dir and type your programs name
Automatik is offline   Reply With Quote
Reply

Tags
addition, printf, problem, scanf, simple

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
Making C DLL using MSVC++ 2005 chico1st C Programming 26 05-28-2008 01:17 PM
We Got _DEBUG Errors Tonto Windows Programming 5 12-22-2006 05:45 PM
ras.h errors Trent_Easton Windows Programming 8 07-15-2005 10:52 PM
Learning OpenGL HQSneaker C++ Programming 7 08-06-2004 08:57 AM


All times are GMT -6. The time now is 09:57 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22