Jump to content

Beginner code problem!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Dubuku57

Dubuku57

    Newbie

  • Members
  • Pip
  • 7 posts
Hi,

This code compiles and runs but the results are not correct. Also, i had to put in getchar() everytime i had to hit enter if not the screen just disappears!

Help pls anyone?

Thanks so much!
-Dub

Below is the simple code:
#include <stdio.h>


int sum(int, int);

int main()

{
    int i,j;
    int add;
   
    printf("Pls enter the digits you would like to add: \n");
    scanf("%d &d", &i,&j);
    getchar();
    
    add = sum(i,j);
    getchar();
   
    printf("This is the sum of the two digits entered: %d", add);
    getchar();
    
 return 0; 
}

int sum(i, j)
{
    int p;
   return p = i + j;
    
}

Edited by xXHalfSliceXx, 30 October 2009 - 04:11 PM.


#2
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
you just made a little typo.

scanf("%d &d", &i,&j);

should be:
scanf("%d %d", &i,&j);

I think after fixing that, you can get rid of the getchar(). It works when I tested it.

#3
Dubuku57

Dubuku57

    Newbie

  • Members
  • Pip
  • 7 posts
Hi sourlemon,

Thanks for pointing it out! I changed it to & but i cant seem to rid the getchar(). If i do, then the window just dissapears after i input the 2 numbers. The printf statement doesnt appear(or rather i cant see it since the window closes too fast...

Is it different for different complilers? Im using dev-c++4.9.9.2...

Thanks lot..

-Dub

#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
:| Think about it. The window launches when you start your program. It should close when it finishes, don't you think? Without the last getchar(), think about it, whats happening? What state is the program in?

#5
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
isnt the function u had declared in the wrong way??

for my compiler i have to declare something like this

int sum(int , int); <-- prototype function

int sum(int i , int j)
{
;
}

TkTech said:

:| Think about it. The window launches when you start your program. It should close when it finishes, don't you think? Without the last getchar(), think about it, whats happening? What state is the program in?

what is the state of the program? how to close it? means stop after finish running ? or close the debug console window after the program finish running??

Edited by TkTech, 30 October 2009 - 11:03 PM.


#6
Dubuku57

Dubuku57

    Newbie

  • Members
  • Pip
  • 7 posts
Hi TkTech,

Yes the program should close when it finishes, but why does the window stay there when there are only printf statements in the code?

It does not print any statement after the sum(i,j) is called. Or rather i cant see it since the window closes too fast.

Im not sure what state you are referring to, but i think when i put the getchar() , it is waiting for the return character.


Hi R3.RyozKidz,

I put int sum(int i, int j) also-and it did not seem to make any difference. Maybe its easier on the compiler in terms of efficiency or smth...is it?

Thanks!
-Dub

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
This is a command line program. it opens a command prompt window in Windows, which is set to close asap the program closes, the program runs so fast that you can't see the final output before it closes.

The solution is to open an command prompt manually (with the cmd command in start->run), change to your program directory and start the program from there.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Dubuku57

Dubuku57

    Newbie

  • Members
  • Pip
  • 7 posts
Hi Orjan,

Yes it works fine using the command line directly. I am curious though, why is it that it only happens when i use scanf? With simple print statements this problem doesnt happen.

Thanks.
-Dub

#9
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
is there any difference if i add argc and argv at the main there?

#10
JustinD

JustinD

    Newbie

  • Members
  • Pip
  • 1 posts
your return statement only needs to be
return(i + j);