Solved

C/C++ -- Console output blinks

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
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.

751 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