Solved

Horizontal Bar-Graph C++

Posted on 2010-11-11
10
1,910 Views
Last Modified: 2013-12-14
I'm a c++ beginner. Can someone PLEASE help me?

Write a program that allows a user to display a horizontal “bar-graph”.
Your program first asks the user to enter a series of positive integers (no more than 20).
They can indicate they are done by entering -1. You should read these numbers into an
array. Be sure you do not allow the user to enter negative numbers. Also make sure the
number entered is less than 70.
Once you have read all the numbers from the user, you are to print a horizontal bar graph,
with each line corresponding to one of the numbers entered. Use iomanip and functions

Ex.
1: 2
2: 4
3: 1

1: **
2: ****
3: *
0
Comment
Question by:cbginner
[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
  • 6
  • 4
10 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34118193
Since this is quite clearly a homework assignment, take a shot at it and post your code when you get stuck. We at EE are not permitted to do your homework for you, but we will certainly help you get it done.
0
 

Author Comment

by:cbginner
ID: 34141177
I apologize, I thought I had added the code, but apparently I didn't lol.

Here is what I got so far.  
#include <iostream>
#include <iomanip>
using namespace std;


int main() {
	const int SIZE = 20;
	char n[SIZE];

	for ( int i = 0; i < SIZE; i++ ) {
		cout << "Please enter a number less than 70 (-1 to stop):  ";
		cin  >> n[i];
	}

	int inputFromUser;
		int n = inputFromUser;

	do	{	

		if ( inputFromUser != -1 ) {
			if ( inputFromUser < 0 || inputFromUser >= 70 ) {
				cout << "Invalid entry" << endl;
			}
			else {
				for ( int i = 0; i < inputFromUser; i++ ) {
					cout << "*" ;
				}
				cout << endl;
			}

		}

	} while ( inputFromUser != -1 ) ;

}

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34141220
There are several things.
You declare n twice as two different things. You can't do this. Take out the second one and it will compile then you can see what it does (it doesn't work yet though)

Your for loop keeps going even if they put in a -1 to stop. This is bad.

In your while loop, you never set inputFromUser to anything. Why not use another for loop? See if you can get it working from this. Fix it up and let me know if you hit another snag. I'll be on for a while so I'll try to answer quickly.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cbginner
ID: 34141435
I took the seconde n out and it still wouldn't compile.

As for the for loop. Do you mean the first one? Should I move it into the do-while loop so it will stop at -1 or create another do while  loop around it?

I'm sorry , but i'm really bad at this so can you try to simplify it even more. Are you suggesting that I use a for loop to replace the inputFromUser function all together?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34141534
What compile error is it giving?
I'm suggesting you do not use a while loop at all just a second for loop. Like this (this is not actual code)

Post the new code too.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34141638
Signing off for a bit. I'll check in with you tomorrow.
0
 

Author Comment

by:cbginner
ID: 34141802
I'm sorry, but I didn't see any example that you posted so I'm still not sure how to go about the while loop for a for loop. Hence, my code hasn't changed much.

As for the compile error, it wasn't even compiling and just said 0 success, but once I clicked rebuild, it was a successful compile. I did also notice that it does not stop when -1 is entered.

Here's the code:

#include <iostream>
#include <iomanip>
using namespace std;


int main() {
	const int SIZE = 20;
	char n[SIZE];

	for ( int i = 0; i < SIZE; i++ ) {
		cout << "Please enter a number less than 70 (-1 to stop):  ";
		cin  >> n[i];
	}

	int inputFromUser;

	do	{	

		if ( inputFromUser != -1 ) {
			if ( inputFromUser < 0 || inputFromUser >= 70 ) {
				cout << "Invalid entry" << endl;
			}
			else {
				for ( int i = 0; i < inputFromUser; i++ ) {
					cout << "*" ;
				}
				cout << endl;
			}

		}

	} while ( inputFromUser != -1 ) ;

}

Open in new window

0
 

Author Comment

by:cbginner
ID: 34141808
* go about replacing the while loop for a for loop
0
 

Accepted Solution

by:
cbginner earned 0 total points
ID: 34170040
I finally came up with the right code, but thank you for your initial help.

#include <iostream>
#include <iomanip>
using namespace std;


void printStars(int numStars ) {
	for ( int i = 0; i < numStars; i++ ) {
		cout << "*" ;
	}
	cout << endl;
}


int main() {
	const int SIZE = 20;
	int n[SIZE];
	int count = 0; // keeps track of how many numbers the user has typed in.

	int inputFromUser;

	do	{	
		cout << "Please enter a number between 0 and 70 (-1 to stop):  ";
		cin  >> inputFromUser;
	
		
		if ( inputFromUser != -1 && ((inputFromUser >= 0) && (inputFromUser <= 70))) {
			// this number is valid. Put inputFromUser into array
			n[count] = inputFromUser;
			count++;
		}

	} while ( inputFromUser != -1 ) ;
		

	for ( int i = 0; i < count; i++ ) {
		
		cout << i + 1 << " - " << setw(2) << n[i] << ": " ; printStars ( n[i] );
			
	}
	

}

Open in new window

0
 

Author Closing Comment

by:cbginner
ID: 34195156
I never received any further responses from my expert.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

717 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