[C++ Help] Simple problem, can't figure it out. menu

Shout-Out

User Tag List

Results 1 to 4 of 4
  1. #1
    Namor's Avatar Active Member
    Reputation
    27
    Join Date
    Jul 2008
    Posts
    311
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C++ Help] Simple problem, can't figure it out.

    Mmk so I was told to make a program that calculates the average of the user input, so I said "Hey, I'll be an overachiever and use for loops."
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int counter2 = 0;
    	int dynamicinput = 0;
    	int scores[1];
    	int average;
    	int sum;
    	for (int x = 0; dynamicinput == 999; x++)
    	{
    		cout << "Input one score." << endl;
    		counter2 + 1;
    		cin >> scores[x - 1];
    		dynamicinput = scores[x - 1];
    	}
    	if(dynamicinput == 999)
    {
    	for (int i = 0; i < counter2; ++i)
    	{
    	        sum+=scores[i];
    	    average = sum/counter2;
    	}
    }
    	return 0;
    }
    is what I have.

    Though it doesn't quite work.

    Visual Studio 2010 output:
    Code:
    verages.exe': Loaded 'C:\Users\Namor\Documents\Visual Studio 2008\Projects\averages\Debug\averages.exe', Symbols loaded.
    'averages.exe': Loaded 'C:\Windows\System32\ntdll.dll'
    'averages.exe': Loaded 'C:\Windows\System32\mscoree.dll'
    'averages.exe': Loaded 'C:\Windows\System32\kernel32.dll'
    'averages.exe': Loaded 'C:\Windows\System32\KernelBase.dll'
    'averages.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcp90d.dll'
    'averages.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcr90d.dll'
    'averages.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcm90d.dll'
    'averages.exe': Loaded 'C:\Windows\System32\ole32.dll'
    'averages.exe': Loaded 'C:\Windows\System32\msvcrt.dll'
    'averages.exe': Loaded 'C:\Windows\System32\gdi32.dll'
    'averages.exe': Loaded 'C:\Windows\System32\user32.dll'
    'averages.exe': Loaded 'C:\Windows\System32\lpk.dll'
    'averages.exe': Loaded 'C:\Windows\System32\usp10.dll'
    'averages.exe': Loaded 'C:\Windows\System32\rpcrt4.dll'
    'averages.exe': Loaded 'C:\Windows\System32\imm32.dll'
    'averages.exe': Loaded 'C:\Windows\System32\msctf.dll'
    'averages.exe': Loaded 'C:\Windows\System32\advapi32.dll'
    'averages.exe': Loaded 'C:\Windows\System32\sechost.dll'
    'averages.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll'
    'averages.exe': Loaded 'C:\Windows\System32\shlwapi.dll'
    'averages.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll'
    'averages.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.4927_none_d08a205e442db5b5\msvcr80.dll'
    'averages.exe': Loaded 'C:\Windows\System32\shell32.dll'
    'averages.exe': Loaded 'C:\Windows\System32\profapi.dll'
    'averages.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\Culture.dll'
    'averages.exe': Unloaded 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\Culture.dll'
    'averages.exe': Loaded 'C:\Windows\assembly\NativeImages_v2.0.50727_32\mscorlib\f58ab951b57c8526430486dcf7ee38fd\mscorlib.ni.dll'
    'averages.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'
    'averages.exe': Loaded 'C:\Windows\System32\cryptbase.dll'
    'averages.exe' (Managed): Loaded 'c:\Users\Namor\Documents\Visual Studio 2008\Projects\averages\Debug\averages.exe', Symbols loaded.
    'averages.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorjit.dll'
    'averages.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\diasymreader.dll'
    'averages.exe': Loaded 'C:\Windows\System32\cryptsp.dll'
    'averages.exe': Loaded 'C:\Windows\System32\rsaenh.dll'
    'averages.exe' (Managed): Loaded 'C:\Windows\WinSxS\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcm90d.dll'
    'averages.exe': Loaded 'C:\Windows\assembly\NativeImages_v2.0.50727_32\System\500ddd904b1099f95552a81b54223b7f\System.ni.dll'
    'averages.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll'
    The thread 'Win32 Thread' (0x1168) has exited with code 0 (0x0).
    The thread 'Win32 Thread' (0x1434) has exited with code 0 (0x0).
    The thread 'Win32 Thread' (0x175c) has exited with code 0 (0x0).
    The program '[3104] averages.exe: Managed' has exited with code 0 (0x0).
    The program '[3104] averages.exe: Native' has exited with code 0 (0x0).
    When I debug.

    So I need some help with the code. Any takers?

    Editable signature, at last!

    [C++ Help] Simple problem, can't figure it out.
  2. #2
    Nikentic's Avatar Elite User
    Reputation
    453
    Join Date
    Oct 2007
    Posts
    1,556
    Thanks G/R
    10/4
    Trade Feedback
    6 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code should not return anything at all.
    for (int x = 0; dynamicinput == 999; x++)

    So, when dynamicinput is 999 it does etc etc. But dynamicinput is never changed from 0 to 999. So does nothing.

    for (int i = 0; i < counter2; ++i)
    couter2 is 0. So if 0 < 0, do that. But 0 is not greater than 0. So does nothing.

  3. #3
    Namor's Avatar Active Member
    Reputation
    27
    Join Date
    Jul 2008
    Posts
    311
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh. I was thinking that for loops were like do whiles. My bad.

    ---------- Post added at 05:30 PM ---------- Previous post was at 05:25 PM ----------

    Now it's working quite well. Thank you for your help, although I have one problem.
    I have it set to where when you type "999" it ends. Though it also adds a new value to the array. I want it to not add in the 999. How might I do that?

    ---------- Post added at 07:08 PM ---------- Previous post was at 05:30 PM ----------

    After several frustrating hours, I finally remade the program.
    Code:
    /*
    Kevin Minehart
    Display average of user input.
    */
    
    
    
    //Uses the Input Output stream header file from the standard library, or "std"
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int scores[100];
    	int amountofplayers = 0;
    	int sum = 0;
    	int average = 0;
    	cout << "How many players do you have?" << endl;
    	cin >> amountofplayers;
    	for(int x = 0; x < amountofplayers; x++)
    	{
    		if(x == 0)
    		{
    			cout << "What was the 1st person's score?" << endl;
    		}
    		if(x == 1)
    		{
    			cout << "What was the 2nd person's score?" << endl;
    		}
    		if(x == 2)
    		{
    			cout << "What was the 3rd person's score?" << endl;
    		}
    		if(x != 0 && x != 1 && x !=2)
    		{
    			cout << "What was the " << x + 1  << "th person's score?" << endl;
    		}
    		cin >> scores[x - 1];
    	}
    	for(int y = 0; y < amountofplayers; y++)
    	{
    		sum = sum + scores[y - 1];
    	}
    	cout << "The average of your scores is about:  " << sum / amountofplayers << endl;
    }
    You're limited to 100 values, but OH WELL. Who's gonna need 100 values anyways?

    Editable signature, at last!

  4. #4
    LaoArchAngel's Avatar Active Member
    Reputation
    47
    Join Date
    Apr 2009
    Posts
    63
    Thanks G/R
    1/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You got it pretty close, but there's an easy way you can bypass your 100# limit. Also, if you're going to limit the count of players to 100, you need to let the user know that. If your user had typed in 101 (a valid option in your code), the app would've thrown an exception later on while assigning values to your array.

    NOTE: I did not change this in an IDE or compile it, so there might be a minor bug or two, but the concept is the same. BTW, coding in a reply box blows balls.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int amountofplayers = 0;
    	int newVal = 0;
    	int sum = 0;
    	int average = 0;
    	cout << "How many players do you have?" << endl;
    	cin >> amountofplayers;
    
    	for(int x = 0; x < amountofplayers; x++)
    	{
    		// You probably haven't learned switch statements yet, but you'll noticed how it easily resolves multiple IF cases into one.
    		// They generally make your code look cleaner and more professional (read: more maintainable).  Also, only one IF case need to be tested here.
    		// 4 If-statements won't eat almost any CPU cycles, but get "optimization" into your head now and you'll be a happy camper in the future.
    		switch ( x )
    		{
    			case 0:
    				cout << "What was the 1st person's score?" << endl;
    				break;  // Break tells the code to not process the next case as well.  Removing these can be used in a cascading effect which may or may not be desirable.
    			case 1:
    				cout << "What was the 2nd person's score?" << endl;
    				break;
    			case 2:
    				cout << "What was the 3rd person's score?" << endl;
    				break;
    			default:
    				cout << "What was the " << x + 1  << "th person's score?" << endl;
    				break;
    		}
    
    		cin >> newVal;   // I'm sure there's a way to combine this line and the next into one, but for the sake of simplicity...
    		sum += newVal;  // += is a binary operator that means add the following value (newVal) to the preceding (sum).
    		// This will get rid of your limited array, as well as the need for a for-loop later on.
    	}
    
    	// Getting rid of the loop you had here is important.  Not in this case, per say, but in more complex apps, additional loops just eat CPU cycles and memory.
    	cout << "The average of your scores is about:  " << sum / amountofplayers << endl;
    
    	return 0;
    }

Similar Threads

  1. Wanted! Can anyone figure this out?
    By Notworking in forum Star Wars: The Old Republic
    Replies: 6
    Last Post: 05-01-2012, 12:05 AM
  2. [Help]Can't figure out how to update models.
    By Babek in forum WoW ME Questions and Requests
    Replies: 1
    Last Post: 10-27-2009, 02:53 PM
  3. Language Problem can not speak on server at all anymore ? Help plz +reps
    By september in forum WoW EMU Questions & Requests
    Replies: 0
    Last Post: 01-14-2009, 10:16 PM
  4. Very simple problem with Wamp - I cant get my server site up - please help
    By faxmunky in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 04-19-2008, 02:59 AM
  5. simple problem need help please..
    By glitch in forum World of Warcraft Emulator Servers
    Replies: 7
    Last Post: 02-24-2008, 09:51 AM
All times are GMT -5. The time now is 07:57 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search