neewb's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Active Members
Active Posts:
65 (0.12 per day)
Joined:
16-October 12
Profile Views:
409
Last Active:
User is offline Mar 17 2014 06:15 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   neewb has not set their status

Posts I've Made

  1. In Topic: Input Validation Not Working

    Posted 16 Mar 2014

    I figured it out and much thanks to ChrisNT for pointing out the first mistake which led to me on finding my second one.

    import javax.swing.JOptionPane;
    
    public class ConversionProgram 
    {
    	public static void main (String [] args)
    	{
    	
    		String input;
    	
    		input = JOptionPane.showInputDialog ("Please enter a distance in meters ");
    		double distance = Double.parseDouble (input);
    		
    		while (distance < 0)
    		{
    			input = JOptionPane.showInputDialog ("Invalid entry\n\nPlease enter a number greater than zero ");
    			distance = Double.parseDouble (input);
    		}
    		
    			menu ();
    			String stringInput;
    			stringInput = JOptionPane.showInputDialog ("Please enter your choice\n\n ");
    			int selection = Integer.parseInt (stringInput);
    			
    			do
    			{
    				
    				switch (selection)
    				{
    					case 1: showKilometers (distance);
    							menu ();
    							stringInput = JOptionPane.showInputDialog ("Please enter your choice ");
    							selection = Integer.parseInt (stringInput);
    					break;
    				
    					case 2: showInches (distance);
    							menu ();
    							stringInput = JOptionPane.showInputDialog ("Please enter your choice ");
    							selection = Integer.parseInt (stringInput);
    					break;
    				
    					case 3: showFeet (distance);
    							menu ();
    							stringInput = JOptionPane.showInputDialog ("Please enter your choice ");
    							selection = Integer.parseInt (stringInput);
    					break;
    				
    					case 4: JOptionPane.showMessageDialog (null, "This program has now ended\nGoodbye");
    							System.exit (0);
    					break;
    						
    					default: stringInput = JOptionPane.showInputDialog ("Invalid choice\n\nPlease enter either 1, 2, 3, or 4 ");
    						 	 selection = Integer.parseInt (stringInput);
    						 
    				} 
    			}	while (selection >= 0 || selection <= 0);
    	}
    
    	public static void menu ()
    	{
    		JOptionPane.showMessageDialog (null, "1. Convert to kilometers\n2. Convert to inches\n3. Convert to feet\n4. Quit the program");
    		
    	}
    	
    	public static void showKilometers (double meters)
    	{
    		double kilometers = meters * 0.001;
    		JOptionPane.showMessageDialog (null, meters + " meters = " + kilometers + " kilometers.");
    	}
    	
    	public static void showInches (double meters)
    	{
    		double inches = meters * 39.37;
    		JOptionPane.showMessageDialog (null, meters + " meters = " + inches + " inches.");
    	}
    	
    	public static void showFeet (double meters)
    	{
    		double feet = meters * 3.281;
    		JOptionPane.showMessageDialog (null, meters + " meters = " + feet + " feet.");
    	}									
    }
    
    
  2. In Topic: Input Validation Not Working

    Posted 16 Mar 2014

    Yup...I didn't catch that. I should have known better there, thank you.
  3. In Topic: Input Validation Not Working

    Posted 16 Mar 2014

    View Postjmsutton334, on 16 March 2014 - 10:07 PM, said:

    Have you debugged it yet?


    Yes, and still couldn't figure out what is wrong.
  4. In Topic: A Function that needs to return a value that has perfect scores

    Posted 25 Jun 2013

    View Postjjl, on 25 June 2013 - 12:51 PM, said:

    Yes! remember that variables do not intialize themselves when declared, so make sure you initialize the "perfect" variable to zero before you enter the for loop.


    Yeah, I figured that out, when I kept getting the wrong total of perfect scores initially.

    Here is what I came up with, it compiles and calculates correctly now:

    #include <iostream>
    using namespace std;
    
    int countPerfect(int intArray[], int);          // Function prototype
    
    int main()
    {
    	const int TEST_SCORES = 10;
    	int testScores[TEST_SCORES],             // Holds test scores for 10 tests
    		perfect;
    
    	cout << "Please enter " << TEST_SCORES << " test scores\n";
    	cout << endl;
    
    	for(int index = 0; index < TEST_SCORES; index ++)
    	{
    		cout << "Test " << (index + 1) << ": ";
    		cin >> testScores[index];
    		cout << endl;
    
    		while((testScores[index] < 0) || (testScores[index] > 100))
    		{ 
    			cout << "Invalid score, please enter a number greater than 0 or less than 100 \n";
    			cin >> testScores[index];
    		}
    	}
    
    	cout << "Here are the scores you entered\n";
    	cout << endl;
    	
    	for(int index = 0; index < TEST_SCORES; index ++)
    	{
    		cout << "Test " << (index + 1) << ": " << testScores[index] << endl;
    	}
    
    	cout << endl;
    	
    	perfect = countPerfect(testScores, TEST_SCORES);
    	if(perfect > 0)        // I added this just in case there were no perfect scores entered at all
    	{
    		cout << "The total number of perfect scores was: " << perfect << endl;
    	}
    	else
    	{
    		cout << "There were no perfect scores entered" << endl;
    	}
    	cout << endl;
    
    	system("pause");
    	return 0;
    }
    
    int countPerfect(int nums[], int numScore)      // This function is now complete and calculates correctly
    {
    	int perfect = 0;       // I initialize the return value here
    	for(int index = 0; index < numScore; index ++)
    	{
    		
    			if(nums[index] == 100)
    				perfect ++;
    	}
    	
    	return perfect;
    }
    
    
  5. In Topic: A Function that needs to return a value that has perfect scores

    Posted 25 Jun 2013

    Quote

    Your naming is somewhat convoluted, the grade parameter is the number of elements in your nums array. If you want to check each grade and compare that grade to the 100 score, then you need to index your nums array with your iterator.


    Do you mean like this?

    if(nums[index] == 100)
    


    Quote

    Also, you are supposed to be counting perfect scores, not returning after the first one. Create a counter and increment it whenever a perfect score is found. At the end of the function, return that counter.


    And this?

    for(int index = 0; index < numScore; index ++)
    	{
    		
    			if(nums[index] == 100)
    				perfect ++;
    	}
    	
    	return perfect;
    }
    

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

neewb hasn't added any friends yet.

Comments

neewb has no profile comments yet. Why not say hello?