Help with making this variable?

Pages: 12
Jul 13, 2012 at 4:04am
Hey guys, im just trying to do a simple variable in netbeans for c++... I only started on Hello World like an hour ago. This is what I'm trying to do:


#include <iostream>

using namespace std;

int main() {
int itemsOnHand= 5;
double itemPrice =3.50;
double total;

total = itemsOnHand * itemPrice;

cout << "The Total is: " << total;


return 0;
}


http://i.imgur.com/psTF7.jpg

I've entered everything, and have ran the program. It runs successfully, but the answer comes out as blank. Does anyone know why? There are no errors, but the answer is blank. If there is anyway to fix this, let me know!


Thank you!
Jul 13, 2012 at 4:11am
Hello Begotten,

There's obviously nothing wrong with the code I had it run myself. Did the console give any output with your "Hello World!" program?
Jul 13, 2012 at 4:33am
Thank you for Replying so quickly! Hello World worked the first time I did it, so I assumed there was something wrong with my code, but it's showing the same result now.

http://i.imgur.com/hnxVA.jpg
Jul 13, 2012 at 4:37am
Hmm, this is just a hunch, but I'm willing to bet that a black box pops up, flashes, then closes.
Try putting system("pause"); on the line above return 0;
To support this command you also need to include stdlib.h
Try...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> 
#include <stdlib.h>

using namespace std; 

int main() { 
int itemsOnHand= 5; 
double itemPrice =3.50; 
double total; 

total = itemsOnHand * itemPrice; 

cout << "The Total is: " << total; 

system("pause");
return 0; 
} 
Jul 13, 2012 at 4:44am
that box definitely pops up! I assumed that happens every time, I'm stupid.

I did it with system("pause") and #include <stdlib.h>; and I have the same result.

I'm wondering, I had to uninstall and reinstall cygwin once, is that why? I've tried restarting netbeans but that doesn't work at all.
Last edited on Jul 13, 2012 at 4:51am
Jul 13, 2012 at 4:54am
Are you using linux?
Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> 
#include <conio.h>

using namespace std; 

int main() { 
int itemsOnHand= 5; 
double itemPrice =3.50; 
double total; 

total = itemsOnHand * itemPrice; 

cout << "The Total is: " << total; 

getch();
return 0; 
} 
Jul 13, 2012 at 5:05am
no, im on windows 7 64, I don't know much about linux other than that CS majors love it. I'm only starting college as a freshman in Computer Science in November, and I don't want to waste this summer. Can you tell me more about linux?

I have the same results with your code too.

http://i.imgur.com/GwiLm.jpg

I'm thinking about uninstalling netbeans and reinstalling but that seems like it will take forever.
Jul 13, 2012 at 5:13am
Hmm, that's strange...
What do the red ! beside the two new lines say?
Jul 13, 2012 at 5:27am
cannot find include file <conio.h>

Unresolved directive #include <conio.h>
Analyzed system include paths:
C:\cygwin\usr\lib\gcc\i686-pc-cygwin\3.4.4\include\c++
C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++
C:\cygwin\usr\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\i686-pc-cygwin
C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\i686-pc-cygwin
C:\cygwin\usr\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\backward
C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\backward
C:\cygwin\usr\lib\gcc\i686-pc-cygwin\3.4.4\include
C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include
C:\cygwin\usr\include
C:\cygwin\usr\include\w32api
C:\cygwin\include\w32api

Ctrl+Alt+Click to open include hierarchy




and, unable to solve identified getch
Jul 13, 2012 at 5:33am
Oh dear...
Did it do the same thing for stdlib.h ?
Jul 13, 2012 at 5:36am
yea, no exclamation points though. Just blank.
Jul 13, 2012 at 5:39am
Hmm, try this one again and make sure you recompile?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> 
#include <stdlib.h>

using namespace std; 

int main() { 
int itemsOnHand= 5; 
double itemPrice =3.50; 
double total; 

total = itemsOnHand * itemPrice; 

cout << "The Total is: " << total; 

system("pause");
return 0; 
}
Jul 13, 2012 at 5:42am
hmm, this one actually failed. This is what I got when I ran it again:

"/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/Wazit/Documents/NetBeansProjects/Beginner'
"/usr/bin/make" -f nbproject/Makefile-Release.mk dist/Release/Cygwin-Windows/beginner.exe
make[2]: Entering directory `/cygdrive/c/Users/Wazit/Documents/NetBeansProjects/Beginner'
mkdir -p dist/Release/Cygwin-Windows
g++ -o dist/Release/Cygwin-Windows/beginner build/Release/Cygwin-Windows/main.o build/Release/Cygwin-Windows/newfile2.o
nbproject/Makefile-Release.mk:62: recipe for target `dist/Release/Cygwin-Windows/beginner.exe' failed
make[2]: Leaving directory `/cygdrive/c/Users/Wazit/Documents/NetBeansProjects/Beginner'
build/Release/Cygwin-Windows/newfile2.o:newfile2.cpp:(.text+0x0): multiple definition of `_main'
nbproject/Makefile-Release.mk:59: recipe for target `.build-conf' failed
build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0x0): first defined here
make[1]: Leaving directory `/cygdrive/c/Users/Wazit/Documents/NetBeansProjects/Beginner'
collect2: ld returned 1 exit status
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
make[2]: *** [dist/Release/Cygwin-Windows/beginner.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 717ms)
Jul 13, 2012 at 5:47am
Ok, that one left my head spinning.
My only suggestion to that would be to try the classic Windows troubleshooting: Restart your pc. I know sometimes my compiler starts acting weird, and a quick restart fixes it. If it still doesn't work, then I'm clueless... :/
Last edited on Jul 13, 2012 at 5:47am
Jul 13, 2012 at 5:52am
ill try that now, but, can you tell me more about linux?
Jul 13, 2012 at 5:58am
I actually know very little about linux. I was just asking because I do not know if the library's I was referencing would work on linux. Google is an excellent resource though ;)
Jul 13, 2012 at 6:01am
oh. Well, the restart failed.
Jul 13, 2012 at 6:04am
Hmm, try recompiling your original code?
If that works, I may have included the wrong library or messed up the cpy/pst.
If it doesn't work, then your compiler is probably messed up. I am not familiar with Netbeans, so I can not help you troubleshoot that.
Jul 13, 2012 at 6:23am
haha, nope. http://i.imgur.com/8pfB1.jpg

im going to have to uninstall and reinstall I guess
Jul 13, 2012 at 6:25am
That code compiled without errors? It just didn't display.
Pages: 12