9 Replies - 5328 Views - Last Post: 31 May 2011 - 03:58 PM Rate Topic: -----

#1 Jgiles21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-May 11

Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 02:19 PM

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

int const SIZE = 10;

int getValidInput(string driverName[], double judgesScores[][SIZE], double degreeDifficulty[])
{
	int length = 0;
	
	cout<<"Enter Driver Name, or ctrl z to exit: ";
	getline(cin, driverName[length]);

	while(!cin.eof())
	{
			for(int ctr = 0; ctr <= 4; ctr++)
			{
				cout<<"Enter the score given by the jodges: ";
				cin>>judgesScores[length][ctr];

				while(judgesScores[length][ctr] <= 0 || judgesScores[length][ctr] > 10.0 || cin.fail())
				{	
					cin.clear();
					cout<<"Scores mus be between 1.0 and 10.0: ";
					cin>>judgesScores[length][ctr];
				}
			}	
		
		cout<<"What was the degree of difficulty: ";
		cin>>degreeDifficulty[length];
		
		while(degreeDifficulty[length] <= 0 || degreeDifficulty[length] > 1.67 || cin.fail())
		{
			cin.clear();
			cout<<"Degree of difficulty must be between 1.0 and 1.67: ";
			cin>>degreeDifficulty[length];
		}
		
		length ++;
		
		cin.ignore(80, '\n');
		cin.clear();
		cout<<"Enter Driver name, or ctrl z to exit: ";
		getline(cin, driverName[length]);

	}
	return length;
}

void addScores(double driverScore[], double judgesScores[][SIZE], int length)
{
	double totalScore = 0;
	
		for(int ctr = 0; ctr <= 4; ctr++)
		{
			totalScore += judgesScores[length][ctr];
		}
	
		driverScore[length] = totalScore;
		cout<<driverScore[length];
}

int main()
{
	cout<<fixed;

	string driverName[SIZE];
	double driverScore[SIZE];
	double judgesScores[5][SIZE];
	double degreeDifficulty[SIZE];
	int length = 0;

	length = getValidInput(driverName, judgesScores, degreeDifficulty);

	//while(length != 0)
	//{
		addScores(driverScore, judgesScores, length);
	//}

	system("Pause");
}


This post has been edited by ButchDean: 31 May 2011 - 02:43 PM
Reason for edit:: Please use code tags!


Is This A Good Question/Topic? 0
  • +

Replies To: Outputs garbage to the screen, like theres nothing in the array

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 02:43 PM

Could you be more descriptive?
Was This Post Helpful? 0
  • +
  • -

#3 Jgiles21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-May 11

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 02:46 PM

when i print out the contents of the array, in the addScores function, there is a huge jumble of negative numbers, or garbage inside the array
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 02:49 PM

That is because you haven't initialized your arrays, hence the garbage. Also you are passing your arrays around by value meaning that once you return from the function all data is lost.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 02:59 PM

HINT: What is the value of length when you pass it into addScores?
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,491
  • Joined: 19-February 09

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 03:10 PM

Also, you declare

double judgesScores[5][SIZE];



and pass it

int getValidInput( string driverName[], 
                   double judgesScores[][SIZE], 
                   double degreeDifficulty[])




and then use the subscripts the other way round.

cin>>judgesScores[length][ctr];


Was This Post Helpful? 0
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 03:22 PM

View PostButchDean, on 31 May 2011 - 02:49 PM, said:

That is because you haven't initialized your arrays, hence the garbage. Also you are passing your arrays around by value meaning that once you return from the function all data is lost.

That's not true at all. If you pass an array to a function, you don't really pass it by value, since it will be implicitly converted to pointer. If you want to pass it by value - you'll have to pack it into a struct - then pass the struct by value. Consider the code below:

//c++0x
#include <iostream>

void modify(int array[], size_t len) {
// will behave the same as: 
//void modify(int* array, size_t len) {
    for (size_t i = 0; i < len; ++i) {
        array[i] = -1;
    }
}

struct Foo {
    int array[5];
};

void try_to_modify(Foo foo) {
    for (int& i : foo.array) {
        i = -1;
    }
}

int main() {
    using std::cout;
    
    int array[5] = { 1, 2, 3, 4, 5 };
    modify(array, 5);
    for (int i : array) {
        cout << i << ' ';
    }
    cout << '\n';
    
    
    Foo foo = { { 1, 2, 3, 4, 5 } };
    try_to_modify(foo);
    for (int i : foo.array) {
        cout << i << ' ';
    }
    cout << '\n';
    
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 03:35 PM

for (int& i : foo.array) {


WHOA. New syntax? Neato!
Was This Post Helpful? 0
  • +
  • -

#9 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 03:43 PM

Well, it's C++0x ranged for, once you start to use it you forget to stop when typing out a simple example on the boards. ;)

You can pretty much use the first for loop and copy paste it with minimal changes to make the example working in C++03 compiler.
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Outputs garbage to the screen, like theres nothing in the array

Posted 31 May 2011 - 03:58 PM

View PostXupicor, on 31 May 2011 - 11:22 PM, said:

View PostButchDean, on 31 May 2011 - 02:49 PM, said:

That is because you haven't initialized your arrays, hence the garbage. Also you are passing your arrays around by value meaning that once you return from the function all data is lost.

That's not true at all. If you pass an array to a function, you don't really pass it by value, since it will be implicitly converted to pointer. If you want to pass it by value - you'll have to pack it into a struct - then pass the struct by value. Consider the code below:

//c++0x
#include <iostream>

void modify(int array[], size_t len) {
// will behave the same as: 
//void modify(int* array, size_t len) {
    for (size_t i = 0; i < len; ++i) {
        array[i] = -1;
    }
}

struct Foo {
    int array[5];
};

void try_to_modify(Foo foo) {
    for (int& i : foo.array) {
        i = -1;
    }
}

int main() {
    using std::cout;
    
    int array[5] = { 1, 2, 3, 4, 5 };
    modify(array, 5);
    for (int i : array) {
        cout << i << ' ';
    }
    cout << '\n';
    
    
    Foo foo = { { 1, 2, 3, 4, 5 } };
    try_to_modify(foo);
    for (int i : foo.array) {
        cout << i << ' ';
    }
    cout << '\n';
    
    return 0;
}

I stand corrected. I knew that but my brain shorted out. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1