CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2015
    Posts
    21

    Finding a total using for loops

    Hello, I was just wondering if there was a way to code in a command that calculates a total number from the given bar chart I made. I'm using for loops. Thanks so much in advance guys!
    Code:
    int main()
    {
    	cout << "Microbes Monthly Growth Char 100/cm" << endl;
    	cout << "***************************************" << endl;
    
    	int spec[5]; //array of from microbes from 5 specimen
    	int mic = 0;
    
    	for (int i = 0; i < 5; i++)
    	{
    		cout << "Specimen #" << i + 1 << endl;
    		cin >> mic;
    		spec[i] = mic / 100;
    
    	}
    	system("CLS");
    	for (int c = 0; c < 5; c++)
    	{
    		cout << "Specimen #" << c + 1;
    		for (int f = 0; f < spec[c]; f++)
    		{
    			cout << "*";
    		}
    		cout << endl;
    	}
    
    	
    
    
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: Finding a total using for loops

    Do you want the total of all the microbes displayed in the bar chart?

    Code:
    #include <iostream>
    using namespace std;
    
    const int arrsize = 5;
    
    int main()
    {
    	cout << "Microbes Monthly Growth Char 100/cm" << endl;
    	cout << "***************************************" << endl;
    
    	int spec[arrsize]; //array of from microbes from 5 specimen
    	int mic = 0;
    
    	for (int i = 0; i < arrsize; i++)
    	{
    		cout << "Specimen #" << i + 1 << endl;
    		cin >> mic;
    		spec[i] = mic / 100;
    	}
    
    	//system("CLS");
    	int total = 0;
    
    	for (int c = 0; c < arrsize; c++)
    	{
    		cout << "Specimen #" << c + 1;
    		for (int f = 0; f < spec[c]; f++)
    			cout << "*";
    
    		cout << endl;
    		total += spec[c];
    	}
    
    	cout << endl << "Total " << total << endl;
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  3. #3
    Join Date
    May 2015
    Posts
    21

    Re: Finding a total using for loops

    Hello, I have that down, user types in numbers then it goes to a screen clear and displays microbes through barchart. Was wondering if I could take all values and add them up as a total at the bottom. As in Total Microbes: #

  4. #4
    Join Date
    May 2015
    Posts
    21

    Re: Finding a total using for loops

    Quote Originally Posted by 2kaud View Post
    Do you want the total of all the microbes displayed in the bar chart?

    Code:
    #include <iostream>
    using namespace std;
    
    const int arrsize = 5;
    
    int main()
    {
    	cout << "Microbes Monthly Growth Char 100/cm" << endl;
    	cout << "***************************************" << endl;
    
    	int spec[arrsize]; //array of from microbes from 5 specimen
    	int mic = 0;
    
    	for (int i = 0; i < arrsize; i++)
    	{
    		cout << "Specimen #" << i + 1 << endl;
    		cin >> mic;
    		spec[i] = mic / 100;
    	}
    
    	//system("CLS");
    	int total = 0;
    
    	for (int c = 0; c < arrsize; c++)
    	{
    		cout << "Specimen #" << c + 1;
    		for (int f = 0; f < spec[c]; f++)
    			cout << "*";
    
    		cout << endl;
    		total += spec[c];
    	}
    
    	cout << endl << "Total " << total << endl;
    
    	return 0;
    }
    right, read that wrong, yes to your question, sorry.

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Finding a total using for loops

    Quote Originally Posted by 2kaud View Post
    Do you want the total of all the microbes displayed in the bar chart?
    Apparently, you accumulate scaled down number (for display purposes?); see:
    Code:
    spec[i] = mic / 100;
    I would calculate the total in the first loop, right after reading mic.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    May 2015
    Posts
    3

    Re: Finding a total using for loops

    Nice Solution

  7. #7
    Join Date
    May 2015
    Posts
    21

    Re: Finding a total using for loops

    thanks very much for responding, this helped me out tons!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured