Solved

C/C++ -- Console output blinks

Posted on 2011-02-19
4
1,051 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 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