Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-09-2005, 10:57 AM   #1 (permalink)
maatghandi
Registered User
 
Join Date: Mar 2005
Posts: 2
maatghandi is on a distinguished road
Simple Question

Total newbie here just getting started. I want to output a "bar graph" composed of asteriks (*). The number of asterisks is equal to the value of variable numberOf*.*. I can't find how to do this. Help would be much appreciated!

Code:
#include <iostream>
using namespace std;

int main () {

    int count = 0;
    double score = 0.0;
    int numberOfAs = 0;
    int numberOfBs = 0;
    int numberOfCs = 0;
    int numberOfDs = 0;
    int numberOfFs = 0;

    cout << "Number of A's: " << numberOfAs <<endl;
    cout << "Number of B's: " << numberOfBs <<endl;
    cout << "Number of C's: " << numberOfCs <<endl;
    cout << "Number of D's: " << numberOfDs <<endl;
    cout << "Number of F's: " << numberOfFs <<endl;
    cout << "Enter a score (%) or -1 to end: ";
    cin >> score;

    if (score != -1) {
          // I haven't written the code yet (obviously)
    } else {
         // This is where I want an output of the above numberOf*.* variables in asterik(*) bargraph form (i.e. A's: ***** if the value of numberOfAs = 5)
}
Hope my description isn't confusing,
Maat
maatghandi is offline   Reply With Quote
Old 03-09-2005, 11:46 AM   #2 (permalink)
maatghandi
Registered User
 
Join Date: Mar 2005
Posts: 2
maatghandi is on a distinguished road
Nevermind...here's what I got:

Code:
#include <iostream>
using namespace std;

int main () {

    int countA = 0;
    int countB = 0;
    int countC = 0;
    int countD = 0;
    int countF = 0;
    double score = 0.0;
    int numberOfAs = 0;
    int numberOfBs = 0;
    int numberOfCs = 0;
    int numberOfDs = 0;
    int numberOfFs = 0;

    cout << "Number of A's: " << numberOfAs <<endl;
    cout << "Number of B's: " << numberOfBs <<endl;
    cout << "Number of C's: " << numberOfCs <<endl;
    cout << "Number of D's: " << numberOfDs <<endl;
    cout << "Number of F's: " << numberOfFs <<endl;
    cout << "Enter a score (%) or -1 to end: ";
    cin >> score;

    if (score != -1) {
        while (score != -1) {
            if (score >= 90) {
                numberOfAs++;
            }
            if (score >= 80 && score < 90) {
                numberOfBs++;
            }
            if (score >= 70 && score < 80) {
                numberOfCs++;
            }
            if (score >= 60 && score < 70) {
                numberOfDs++;
            }
            if (score >= 0 && score < 60) {
                numberOfFs++;
            }

            cout << "Number of A's: " << numberOfAs <<endl;
            cout << "Number of B's: " << numberOfBs <<endl;
            cout << "Number of C's: " << numberOfCs <<endl;
            cout << "Number of D's: " << numberOfDs <<endl;
            cout << "Number of F's: " << numberOfFs <<endl;

            cout << "Enter a score (%) or -1 to end: ";
            cin >> score;
        }

    }
    cout << endl;
    cout << "A's: ";
    while (countA < numberOfAs) {
        cout << "*";
        countA++;
    }
    cout << endl;
    cout << "B's: ";
    while (countB < numberOfBs) {
        cout << "*";
        countB++;
    }
    cout << endl;
    cout << "C's: ";
    while (countC < numberOfCs) {
        cout << "*";
        countC++;
    }
    cout << endl;
    cout << "D's: ";
    while (countD < numberOfDs) {
        cout << "*";
        countD++;
    }
    cout << endl;
    cout << "F's: ";
    while (countF < numberOfFs) {
        cout << "*";
        countF++;
    }
    cout << endl;
return 0;
}
Any suggestions on simplifying it? I'm not familiar enough with functions yet to be comfortable with them, but if anyone has suggestions, that would be appreciated!

Maat
maatghandi is offline   Reply With Quote
Old 03-10-2005, 04:38 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Very well done. Let's see...
1) Try it without:
Code:
int countA = 0;
    int countB = 0;
    int countC = 0;
    int countD = 0;
    int countF = 0;
2) Remove the "if" statement.
3) Try this then:
Code:
while (cin>>score)
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question if13121 Standard C, C++ 10 11-01-2004 07:29 AM
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
simple c question if13121 Standard C, C++ 3 10-18-2004 11:20 PM
another simple question cracksevi Standard C, C++ 4 10-18-2004 01:53 AM
Really simple question... Ilya020 HTML, XML, Javascript, AJAX 12 10-29-2003 03:10 PM


All times are GMT -8. The time now is 04:59 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting