What went wrong?

Everything todo with programming goes HERE.
Post Reply
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

What went wrong?

Post by Ratchet »

Out of boredom, I'm attempting to play around with some C++.

However, I've run into an apparent problem.

Code: Select all

#include <iostream>

using namespace std;

int main()
	{
		int width, length, height;
		cout << "What is the value that you'd like to enter for width? ";
		cin >> width;
		cout << "What is the value that you'd like to enter for length? ";
		cin >> length;
		cout << "What is the value that you'd like to enter for height? ";
		cin >> height;
		cout << "Okay! Now, the volume of your object is: " + width * length * height;
		return 0; 
	}

The above [should] be a very simple concept. I went into notepad, wrote it, then saved it to my desktop as Document1.cpp

I have Microfuck Visual Fuckup C++ 2010 Express Edition, just because it's a fun thing to mess around with, particularly the GUI.

I, for the life of me, cannot get my computer to execute a .cpp file alone, apparently it wants something more. However, my current guide doesn't really specify that.

I did, though, figure out that Bloodshed Dev-C++ will allow me to execute my cpp file, and it does it functionally.

A minor complaint, though, is when Winblows runs the .cpp file, it doesn't wait after the final inputs are sent and it doesn't allow the user to see the final cout >> line, and I don't remember how to deal with that.

The program will do something like:

Code: Select all

What is the value you'd like to enter for height? (input)
What is the value you'd like to enter for length? (input)
What is the value you'd like to enter for width? (input)
<Now! Here, the program should output the final line containing the string + volume, but it doesn't, the program shuts down immediately after outputting the line, which isn't what I wish to achieve.>
 
Help? Is the only alternative to require a keypress before return 0; ?
If so, please do share the proper command, which I forgot.


Edit: In regards to MSVSC++, I try to make a Console application, put my code into the .cpp file, and debug it, but that's apparently asking a whole lot. The program seems to depend on many other useless things, making everything complicated. Gotta love Microsoft and their programs..
It even goes as far as to tell me that (even after including the iostream) cout << and cin >> don't exist.
Last edited by Ratchet on Thu Dec 23, 2010 2:31 am, edited 1 time in total.
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

Re: What went wrong?

Post by Ratchet »

Alright. Update:

After much experimenting, I've figured out that instead of declaring the project will be a Console Application ahead of time, I am able to just define the project as an "Empty Project" and create my .cpp file under 'Source Files'.

Below I've attached an image of what each program requires, just for the hell of it.
Attachments
devc++.png
msvs.png
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Re: What went wrong?

Post by Jonathan »

Ratchet wrote:

Code: Select all

cout << "Okay! Now, the volume of your object is: " + width * length * height;
You'll want to use << instead of +. Now you're just adding the volume to the pointer, which is pretty bad. ;-)
ˌɑrməˈɡɛˌtrɑn
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

Re: What went wrong?

Post by Ratchet »

ohmygod. I totally didn't think that through.

/me is a noob.

that solves that problem, I was wondering why it didn't feel safe outputting that line, while at the same time not showing an error.


Fact still stands though; I need some sort of code/command to make the program wait before it terminates itself before I (the user) can read the final outputs.
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Re: What went wrong?

Post by Jonathan »

I'm not that familiar with Windows and its IDEs, but isn't there an option somewhere? Either way, some things you can try:

#include <unistd.h>
and
sleep(5); // just wait for 5 seconds

#include <unistd.h>
and
for (;;) sleep(5); // wait in infinite loop, if you can easily kill it yourself

#include <fstream>
and
ofstream file("path");
file << "save to a file instead";
ˌɑrməˈɡɛˌtrɑn
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

Re: What went wrong?

Post by Ratchet »

I'll try it out in a moment, thanks for you assistance !

and no, even after browsing nearly the entire IntelliSense(Microsoft's included 'glossary') so to speak, I couldn't find anything O.O
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

Re: What went wrong?

Post by Ratchet »

Update:
Not quite working. I will put the code below, then below that what I would expect the program to do; this will allow you to correct any flaws I may visibly have.

Also; I'm getting an error; check attachments.

Code: Select all

#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;

int main()
	{
		int width, length, height, volume;
		cout << "What is the value that you'd like to enter for width? ";
		cin >> width;
		cout << "What is the value that you'd like to enter for length? ";
		cin >> length;
		cout << "What is the value that you'd like to enter for height? ";
		cin >> height;
		volume = width * length * height;
		cout << "Okay! Now, the volume of your object is: " << volume;
		{
		if(volume > 400)
		{
			cout << "This shape's volume is greater than 400.";
		}
		else
		{
			cout << "This shape's volume is less than 400.";
		}
		sleep(10);
		return 0;
		}
	}

Code: Select all

Ask for length. (input)
Ask for width. (input)
Ask for height. (input)
// Calculate volume
Okay! Now the volume of your object is: #
[if >400]Your object's volume is greater than 400
[else]Your object's volume is less than 400
sleep for 10 seconds
end
Attachments
error.png
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
Luke-Jr
Dr Z Level
Posts: 2246
Joined: Sun Mar 20, 2005 4:03 pm
Location: IM: [email protected]

Re: What went wrong?

Post by Luke-Jr »

Just FWIW, it'd be very very very very bad practice to include the sleep at the end of your program. The real solution is to configure your terminal not to exit immediately. I know Windows 98 at least had this option.
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Re: What went wrong?

Post by Jonathan »

Luke-Jr: seems he's just trying to get his first code to run somehow, so it isn't that important.

Ratchet: looks like Windows doesn't have sleep. :-( Maybe it does have usleep? That one takes microseconds, so feed it millions to get seconds.
ˌɑrməˈɡɛˌtrɑn
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

Re: What went wrong?

Post by Ratchet »

Luke-jr: It's okay, thanks for some input, seems as if few people have much to say on the matter ;D. I suppose I'll have to look around for windows' option on the matter
Care to give me any constructive input as to why 'sleep' is a bad thing? Curiosity.
Jonathan: Yeah, I'm just trying to get something put together to execute and mess around with the codes and their functions. I also have ubuntu(dual boot), though; (I believe Luke was the one that convinced me 2? years ago, heh).

I'm going to browse the compilers, see what I can find, and which I'd like.
Curiosity: Are there any critical/major differences in Windows/Ubuntu C++ development? Code Actions? Etc.
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Re: What went wrong?

Post by Jonathan »

Sleeping at the end of a program is bad because it freezes the user's terminal unnecessarily. A forced 5-second wait would get old quickly. But I can't see the harm of it if it's just to see what your first lines of code are doing, when you can't find another way at first. It's still 'wrong', just not that bad in the end.

Can't help you with the rest.
ˌɑrməˈɡɛˌtrɑn
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

Re: What went wrong?

Post by Ratchet »

Okay! Yay ubuntu!

Created an empty project in Qt, built it, and ran it in the terminal! Works!
sample run:

Code: Select all

joshua@joshua-laptop:~$ '/home/joshua/Desktop/ConsoleTesting/ConsoleTesting' 
What is the value that you'd like to enter for width? 23
What is the value that you'd like to enter for length? 14
What is the value that you'd like to enter for height? 28
Okay! Now, the volume of your object is: 9016
Your object's volume is greater than 400. 
I had to make a few minor changes here and there, though. Turns out Ubuntu's terminal doesn't require 'sleep' :D.

Code: Select all

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
       {
                int width, length, height, volume;
                cout << "What is the value that you'd like to enter for width? ";
                cin >> width;
                cout << "What is the value that you'd like to enter for length? ";
                cin >> length;
                cout << "What is the value that you'd like to enter for height? ";
                cin >> height;
                volume = width * length * height;
                cout << "Okay! Now, the volume of your object is: " << volume << "\n";
                     {
                if(volume > 400)
                         {
                        cout << "Your object's volume is greater than 400. \n";
                         }
                else
                         {
                        cout << "Your object's volume is less than 400. \n";
                         }
                return 0;
                    }
        }
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
User avatar
nsh22
Round Winner
Posts: 378
Joined: Fri Sep 07, 2007 8:12 pm
Location: eating, cooking or writing (about cooking).
Contact:

Re: What went wrong?

Post by nsh22 »

heh ur using qt as well? im still learning it for a little side project, care to join?
Lucifer wrote:I think you got the wrong thread, this thread is the one where we're debating banning sinewav and dubStep until they have a threesome with dubbie's mother.
User avatar
Ratchet
Match Winner
Posts: 779
Joined: Sat Mar 15, 2008 5:55 am

Re: What went wrong?

Post by Ratchet »

I've been using Geany for a simple IDE to work on console projects.

However, recently, I've decided to use Code::Blocks instead due to the terrible syntax highlighting of Geany.

I haven't played around much with Qt, I'm just focusing on the actual coding part first :D
Image
"Dream as if you'll live forever,
Live as if you'll die today." -James Dean
Post Reply