Solved

C/C++ -- Console output blinks

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 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.

895 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

12 Experts available now in Live!

Get 1:1 Help Now