C++ help

Post » Tue Jan 22, 2013 4:48 pm

i am writing a small program that will calculate the distance of an abject falling at 32 feet per second for some reason my code is not working correctly, this is what i have so far.

#include
using namespace std;

int main()
{
int seconds;
int distance;

cout << "Enter a time in seconds." << endl;
cin >> seconds;

distance = (32 * seconds);

cout << "An object in freefall for " << seconds << " seconds will fall " << distance << " feet." << endl;

return 0;
}
User avatar
Kirsty Wood
 
Posts: 3461
Joined: Tue Aug 15, 2006 10:41 am

Post » Tue Jan 22, 2013 3:50 pm

What do you mean it's not working? But happens? Compiler error? Logic error? Details are handy.
User avatar
kristy dunn
 
Posts: 3410
Joined: Thu Mar 01, 2007 2:08 am

Post » Tue Jan 22, 2013 4:24 pm

all i am getting is that my _stdout is incorrect except for when the time is 0 seconds
also yes a logic error
User avatar
Craig Martin
 
Posts: 3395
Joined: Wed Jun 06, 2007 4:25 pm

Post » Tue Jan 22, 2013 5:27 pm

Sorry OP, seems my program won't cooperate with me to test things out. Seems i've been out of the loop when coding since i've never even seen a reason for the "using namespace std;" line of code. I just used C++ straight up cin and cout things.

http://www.parashift.com/c++-faq-lite/using-namespace-std.html thing about that using namespace std; thing.

Only difference I see is the FAQ putting the use tag within the main instead of outside. Another thing you might want to do is set seconds and distance = to 0 in order to prevent the program from randomly assigning numbers. *Again, it's been a good eleven years since i've really delved into C++, so I don't know how things work now. Only use what I learned to deconstruct different languages at times, from HTML and CSS to other languages like that Papyrus thing Skyrim uses.*
User avatar
Austin England
 
Posts: 3528
Joined: Thu Oct 11, 2007 7:16 pm

Post » Tue Jan 22, 2013 5:56 pm

One glaring issue is that your variables are not initialized to 0.
User avatar
Devin Sluis
 
Posts: 3389
Joined: Wed Oct 24, 2007 4:22 am

Post » Tue Jan 22, 2013 9:27 pm

set the variables to 0, same errors showing
User avatar
Ilona Neumann
 
Posts: 3308
Joined: Sat Aug 19, 2006 3:30 am

Post » Tue Jan 22, 2013 10:53 pm

What errors? You haven't given us anything to work with except for it's not working. We need details.

We also need to know what compiler you're using.
User avatar
Far'ed K.G.h.m
 
Posts: 3464
Joined: Sat Jul 14, 2007 11:03 pm

Post » Wed Jan 23, 2013 4:53 am

#include
using namespace std;

int main()
{
int X=0;
int Y=32;

cout<<"Enter a time in seconds."<cin>>X;
cout<cout<
cout<<"An object in freefall for "<
return 0;
}


Ok, try these fixes and see if they help you out. The distance = (32*seconds); command maybe a bit redundant when you can just use a constant integer of 32. You could possibly even get rid of Y/distance and just use X*32 since it sounds like 32 is a constant rate that remains unchanged. Also, i've removed the space between the << and the commands to see if that was a problem, but those are probably cosmetic.
User avatar
Veronica Flores
 
Posts: 3308
Joined: Mon Sep 11, 2006 5:26 pm

Post » Wed Jan 23, 2013 6:30 am

Probably something is wrong with your compiler like a setting. Your code works fine in Visual Studio 2010.
User avatar
christelle047
 
Posts: 3407
Joined: Mon Apr 09, 2007 12:50 pm


Return to Othor Games