Solved

C/C++ -- Console output blinks

Posted on 2011-02-19
4
1,031 Views
Last Modified: 2012-05-11
I've come across sample code (see code section below) that I'd like to use as a starting point for a similar program.

Right now, I'm trying to make sense of the code... unfortunately, the console output only "flashes" by.    I've added the following line:
        system("PAUSE");
        return 0;

By adding the >> system("PAUSE"); << I'm not getting a compile error.    What code needs to be added to see the console output?

Thanks,
EEH
#include <stdio.h>

#define MAXWEIGHT 100

int n = 3;						/* The number of objects */
int c[10] = {8, 6, 4};			/* c[i] is the *COST* of the ith object; i.e. what YOU PAY to take the object */
int v[10] = {16, 10, 7};		/* v[i] is the *VALUE* of the ith object; i.e. what YOU GET for taking the object */
int W = 10;						/* The maximum weight you can take */ 



void fill_sack() {
	int a[MAXWEIGHT];			/* a[i] holds the maximum value that can be obtained using at most i weight */
	int last_added[MAXWEIGHT];	/* I use this to calculate which object were added */
	int i, j;
	int aux;

	for (i = 0; i <= W; ++i) {
		a[i] = 0;
		last_added[i] = -1;
	}

	a[0] = 0;
	for (i = 1; i <= W; ++i)
		for (j = 0; j < n; ++j)
			if ((c[j] <= i) && (a[i] < a[i - c[j]] + v[j])) {
				a[i] = a[i - c[j]] + v[j];
				last_added[i] = j;
			}

	for (i = 0; i <= W; ++i)
		if (last_added[i] != -1)
			printf("Weight %d; Benefit: %d; To reach this weight I added object %d (%d$ %dKg) to weight %d.\n", i, a[i], last_added[i] + 1, v[last_added[i]], c[last_added[i]], i - c[last_added[i]]);
		else
			printf("Weight %d; Benefit: 0; Can't reach this exact weight.\n", i);

	printf("---\n");

	aux = W;
	while ((aux > 0) && (last_added[aux] != -1)) {
		printf("Added object %d (%d$ %dKg). Space left: %d\n", last_added[aux] + 1, v[last_added[aux]], c[last_added[aux]], aux - c[last_added[aux]]);
		aux -= c[last_added[aux]];
	}

	printf("Total value added: %d$\n", a[W]);


}



int main(int argc, char *argv[]) {
	fill_sack();

	return 0;

}

Open in new window

0
Comment
Question by:ExpExchHelp
  • 2
4 Comments
 
LVL 3

Expert Comment

by:gopisera
ID: 34933223
add the function  getch();  at the end of your code.  So, you will able to see the out put at the console.

Or   Pressing the Alt+5 gives to view the console output

or you can also view in the windows tab in the turbo c++

or  you can use any one of the functions of getch() or getchar() ...etc., which will take a single valid character as input.  Until then you  can see the output.
0
 

Author Comment

by:ExpExchHelp
ID: 34933253
gopisera:

Don't know where to put the getch().    Could you please provide the code?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 34933428
For 'system("PAUSE")' to compile, add

#include <cstdlib>

or

#include <stdlib.h>

to your code - however, I'd not recommend using it, read here http://www.gidnetwork.com/b-61.html ("Things to Avoid in C/C++ -- system("pause")") why. Also: You are building a console app, so why not calling it from a console? Open a cmd.exe intance, navigate to the folder where your executeble resides and run it.
0
 

Author Comment

by:ExpExchHelp
ID: 34933516
jkr:

I've opened a related question....

http://www.experts-exchange.com/Programming/Languages/CPP/Q_26833598.html

Any chance you could have a look at it?

EEH
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question