Solved

C/C++ -- Console output blinks

Posted on 2011-02-19
4
1,023 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now