![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 3
| Beginner Error 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;
}
Code: Insert a number pl0x: 3 Insert another number pl0x: 3 The two numers entered added together are: 2293580 |
| SvenRL is offline | |
| | #2 | |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Quote:
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 | |
| | #3 | |
| Registered User Join Date: Jul 2009
Posts: 3
| Quote:
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 | |
| | #4 |
| +++ OK NO CARRIER 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 | |
| | #5 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
__________________ 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 | |
| | #6 |
| Registered User 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 | |
![]() |
| Tags |
| addition, printf, problem, scanf, simple |
| Thread Tools | |
| Display Modes | |
|
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 |