why is my program not working

Aug 8, 2010 at 1:27pm
// please work.cpp : Defines the entry point for the console application.
//

/*****************************************************/
/* File: Ehren Williamson final project */
/* */
/* Created by: Ehren Williamson */
/* Date: 8 August 2010 */
/* */
/* Program to compute the weekly wages */
/* */
/* Inputs: (keyboard) */
/* 1. The sum of 10 numbers using an array */
/* */
/* */
/* Output: */
/* Average of the 10 numbers */
/* */
/* Algorithm: average= sum/10 */
/* */
/****************************************************/

#include "stdafx.h"

#include <iostream>

using namespace std ;

int _tmain(int argc, _TCHAR* argv[])
{

int sum = 0 ;
int average = 0 ;
int array[10] = {1,2,3,4,5,6,7,8,9,10} ;
for (int i = 0; i < 10; ++i) sum=sum + array[i] ;
average = sum/10 ;

cout << "The average is: "<< average ;

return 0; //terminates


}


All that happens is a black screen appears with nothing in it
Aug 8, 2010 at 2:40pm
Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int sum = 0;
    int average = 0;
    int array[10] = {1,2,3,4,5,6,7,8,9,10};
    for (int i = 0; i < 10; ++i) sum += array[i];
    average = sum/10;
    cout << "The average is: "<< average;
    return 0;
}
Aug 8, 2010 at 4:58pm
ehren101 wrote:
All that happens is a black screen appears with nothing in it

I'm assuming you're using Dev-C++? Add cin.ignore just before you're return statement.
Last edited on Aug 8, 2010 at 5:00pm
Aug 8, 2010 at 5:04pm
you need to flush the output buffer.

do either of these before you exit:

1
2
cout << endl;  // flushes and puts a new line
cout << flush;  // flushes, no new line 
Aug 8, 2010 at 6:06pm
@Disch... Are you sure that's his problem? Why would he need to flush the output buffer?
Aug 8, 2010 at 7:04pm
none of that worked all the debugger says is

'try this.exe': Loaded 'C:\Users\Ryan\Documents\Visual Studio 2010\Projects\try this\Debug\try this.exe', Symbols loaded.
'try this.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'try this.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'try this.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
'try this.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
The program '[5500] try this.exe: Native' has exited with code 0 (0x0).
Aug 8, 2010 at 7:23pm
@Disch... Are you sure that's his problem?


No. But it was an educated guess.

Why would he need to flush the output buffer?


So that output gets displayed.

Remember that cout is buffered, so just because you output something to it doesn't mean it gets displayed right away. You can output a bunch of stuff to count and it won't be visible until cout internally moves the data from the buffer to the actual display. That's what flushing does.

He said he was just getting a black screen, so it made sense to me that output wasn't flushing.

none of that worked all the debugger says is


So the window closing right away before you can see anything?

Read this: http://cplusplus.com/forum/beginner/1988/
Aug 8, 2010 at 7:58pm
your for loop has no curly braces {}... are you allowed to do that?
Aug 8, 2010 at 8:19pm
He said he was just getting a black screen, so it made sense to me that output wasn't flushing.

Yeah I completely missed that :s wouldn't have asked if I had seen it.

your for loop has no curly braces {}... are you allowed to do that?

Yes but only the first statement thereafter will be executed for as long as the loop is iterated.
Aug 9, 2010 at 12:21pm
Okay, but why is the black screen showing up I can't run the program? I debug it and the output shows a black screen. I can't type any information in to test it. It just pops up and disappears and shows that 0 (0X0). The program is suppose to let the user pick 10 numbers and find the average of them using an array and a function. Any of the ideas on how to test the program.


Aug 9, 2010 at 12:21pm
Okay, but why is the black screen showing up I can't run the program? I debug it and the output shows a black screen. I can't type any information in to test it. It just pops up and disappears and shows that 0 (0X0). The program is suppose to let the user pick 10 numbers and find the average of them using an array and a function. Any of the ideas on how to test the program.


Aug 9, 2010 at 12:37pm
closed account (z05DSL3A)
Assuming that you are using VC express (or similar).
Go to the debug menu and select Start without debugging (and follow any prompts)
Aug 9, 2010 at 1:19pm
Add

cin.get();

before

return 0;

so that the window shows up long enough to see the answer. Press ENTER to quit the program.

or, you can just run the program from command line, with out the above, and it works fine :)

How ever if it's supposed to

The program is suppose to let the user pick 10 numbers


like you say, it won't, as there's nothing in there to let the user pick the numbers.
Last edited on Aug 9, 2010 at 1:21pm
Topic archived. No new replies allowed.