Click here to Skip to main content
13,635,085 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I am receiving a few errors that are confusing to me. I have tried changing variables around, but it has not helped. I will post my code and errors. Any help welcome. I know that my code is not great at all. I honestly am not good at programming at all.
Sorry.

<pre>/*  Program:	prog6.cpp
	By:		Mackenzie Ritter
	Last Modified:	Dec 3, 2017
	Purpose:	To provide a rating for each elf based on their toy production and output all information.
	Notes:
*/
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
 
using namespace std ;
 
string* elfRating (int, int[], string[]) ;
 
int main () 
{
	int SIZE = 50 ;
	string *names [SIZE] ;		// Elf's name
	int numToys [SIZE] ;		// Number of toys made by each elf
	string *rating [SIZE] ;		// Rating for elves based on number of toys
	string line = " " ;
	ifstream ins ; 			
	ins.open ("elves.dat") ;
	int i = 0 ;
	istringstream iss (line) ;
	while (getline(ins, line))
	{
		istringstream iss (line) ;
		iss >> *names [i] >> numToys [i] ;
		i++ ;
	}
	int total = 0 ;
	string* rate = elfRating (SIZE, numToys, rating) ;
	for (int num = 0; num < SIZE; num++)
	{
		total += numToys[num] ;
	}
	int cnt = 0 ;
	int max = numToys [0] ;
	while (cnt < SIZE)
	{
		if (numToys[cnt] > max)
		{
			max = numToys [cnt] ;
			cnt++ ;
		}
		else
		{
			cnt++ ;
		}
	}	
	int count = 0 ;
	int min = numToys [0] ;
	while (count < SIZE)
	{
		if (numToys[count] < min)
		{
			min = numToys [count] ;
			count++ ;
		}
		else
		{
			count++ ;
		}
	}
	int CNT = 0 ;
	int goodElves = 0 ;
	while (CNT < SIZE)
	{
		if (numToys[CNT] > 500)
		{
			goodElves = goodElves + 1 ;
			CNT++ ;
		}
		else 
		{
			CNT++ ;
		}
	}
	int ii = 0 ;
	while (ii < 50)			//printout of arrays
	{
		cout << setw(30) << left << "Elves name:" << names[ii]
			<< setw(20) << left << "Number of Toys Produced:" << numToys[ii]
			<< setw(10) << left << "Rating:" << rating[ii] ;
		ii++ ;
	}
	cout << "Total number of toys created by all elves: " << total ;
	cout << "The number of elves that have produced over 500 toys: " << goodElves ;
	cout << "The elf who made the greatest number of toys: " << names[cnt] ;
	cout << "The elf who made the lowest number of toys: " << names[count] ;
	
	
	cin.ignore () ;
}

/*  Function:		elfRating
	Last Modified:	Dec 3, 2017
	Purpose:		To provide a rating for each elf based on their toy production.
	In Parameters:	None
	Out Parameters:	None
	Return Value:	None 
*/

string* elfRating (int SIZE, int *numToys, string *rating) 
{
	int t = 0 ;
	for (t=0;t<50;t++)
	{
		if (numToys [t] < 200)
		{
			rating [t] = "-" ;
		}
		else if (numToys [t] < 300)
		{
			rating [t] = "*" ;
		}
		else if (numToys [t] < 500)
		{
			rating [t] = "***" ;
		}
		else
		{
			rating [t] = "*****" ;
		}
	}
	return rating ;
}


Here are the errors that I am receiving.

prog6.cpp: In function ‘int main()’:
prog6.cpp:35: error: cannot convert ‘std::string**’ to ‘std::string*’ for argument ‘3’ to ‘std::string* elfRating(int, int*, std::string*)’


What I have tried:

I have tried looking at other posts on different discussion forums, but could not gain any information that would help me.
Posted 5-Dec-17 12:43pm
Updated 7-Dec-17 11:48am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

declare SIZE with macro
as in
#define SIZE 50

the way you have declared elfRating will throw error, either you declare the array as pointer or with literal value; probably c++14 has other way of declaring;

also, you can write if else cascade as follow
if (numToys [t] < 200)
{
    rating = "-" ;
}
else if (numToys [t] < 300)
{
    rating = "*" ;
}
else if (numToys [t] < 500)
{
    rating = "***" ;
}
else
{
    rating = "*****" ;
}

there is no way, you will get different value

in the first while loop of main
you have
cout << names [10] << endl ; // until array index 9, you will get empty, from 10 you will get same value printed;


in the for loop of main function
you have declared
total in the for loop block. total variable does not exists outside the loop. As well as you are setting total back to zero in every loop;

in the elfRating function you have sent rating as std::string array; you are trying to set string in the array; C++ compiler must throw error.
Suggestion is:
1.
change
rating="**** string";
to
rating[t]="**** string";
2.
change elfRating return type to void, remove the return;
3.
in the second while loop, remove the function calling of elfRating
move elfRating above for loop;

you should close ins too;

And I feel like, you haven't learn c++ programming or any other programming very well, you need more study

--edit: add total error fixing and with example--
/*  Program:	prog6.cpp
	By:		Mackenzie Ritter
	Last Modified:	Dec 3, 2017
	Purpose:	To provide a rating for each elf based on their toy production and output all information.
	Notes:
*/
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#define SIZE 50                 // new line
using namespace std ;
 
void elfRating (int[], string[]) ; // modified
 
int main () 
{
	//int SIZE = 50 ;               // removed
	string names [SIZE] ;		// Elf's name
	int numToys [SIZE] ;		// Number of toys made by each elf
	string rating [SIZE] ;		// Rating for elves based on number of toys
	string line = " " ;
	ifstream ins ; 			
	ins.open ("elves.dat") ;
	int i = 0 ;
	istringstream iss (line) ;
	while (getline(ins, line))
	{
		istringstream iss (line) ;
		iss >> names [i] >> numToys [i] ;
		i++ ;
	}
	int total = 0 ;
	elfRating (numToys, rating) ; // edited
	for (int num = 0; num < SIZE; num++)
	{
		total += numToys[num] ;
	}
	int cnt = 0 ;
	int max = numToys [0] ;
	while (cnt < SIZE)
	{
		if (numToys[cnt] > max)
		{
			max = numToys [cnt] ;
			cnt++ ;
		}
		else
		{
			cnt++ ;
		}
	}	
	int count = 0 ;
	int min = numToys [0] ;
	while (count < SIZE)
	{
		if (numToys[count] < min)
		{
			min = numToys [count] ;
			count++ ;
		}
		else
		{
			count++ ;
		}
	}
	int ii = 0 ;
	while (ii < 50)			//printout of arrays
	{
		
		cout << setw(30) << left << "Elves name:" << names[ii]
			<< setw(20) << left << "Number of Toys Produced:" << numToys[ii]
			<< setw(10) << left << "Rating:" << rating[ii] ;
		ii++ ;
	}
	cout << "Total number of toys created by all elves: " << total ;
	cout << "The greatest number of toys created is: " << max ;
	cout << "The lowest number of toys created is: " << min ;
	
	cin.ignore () ;
}
 
/*  Function:		elfRating
	Last Modified:	Dec 3, 2017
	Purpose:		To provide a rating for each elf based on their toy production.
	In Parameters:	None
	Out Parameters:	None
	Return Value:	None 
*/
 
void elfRating (int numToys [SIZE], string rating [SIZE]) 
{
	int t = 0 ;
	for (t=0;t<50;t++)
	{
		if (numToys [t] < 200)
		{
			rating [t] = "-" ;
		}
		else if (numToys [t] < 300)
		{
			rating [t] = "*" ;
		}
		else if (numToys [t] < 500)
		{
			rating [t] = "***" ;
		}
		else
		{
			rating [t] = "*****" ;
		}
	}
}
  Permalink  
v3
Comments
5-Dec-17 21:09pm
   
I know, I am not good at this at all. Thank you very much though for all this help.
Where did you mean for me to move the calling to elfRating?
Mohibur Rashid 5-Dec-17 22:45pm
   
right above second while loop, because elfRrating function is setting the rating array, it doing it at once.
By the way, there are few other logical error, but I guess you will learn with time
6-Dec-17 7:42am
   
Thank you. I moved it. I am still getting the errors that I listed above for line 61, but I am sure that you helped me clean it up.
Would you mind sharing a few other errors with me? If not, thank you for your help already.
Mohibur Rashid 6-Dec-17 16:32pm
   
Update your code
Mohibur Rashid 7-Dec-17 5:09am
   
I have updated answer, this does not guarantee that your logic is correct. I didn't check your logic. I Just fixed the error. Please debug and find it out
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hey man, it is C++. You have a powerful standard library.

The following program gets data from standard input, use it this way:
elfrating < elves.dat



#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

struct ElfInfo
{ 
  string name;
  int toys;
  
  string to_string() const
  { 
    ostringstream oss; 
    oss << setw(30) << left << "Elf name:" << name
      << setw(20) << left << " Number of Toys Produced:" << toys
      << setw(10) << left << " Rating:" << rating();
    return oss.str();
  }
  string rating() const
  { 
    if (toys < 200 )
      return "-";
    else if ( toys < 300)
      return "*"; 
    else if (toys < 500)
      return "***";
    else
      return "*****";
  }
};

int main()
{
  vector <ElfInfo> vei;
  string line;
  while ( getline(cin, line))
  {
    ElfInfo ei;
    istringstream iss(line);
    iss >> ei.name >> ei.toys;
    vei.push_back(ei);
  }

  int total_toys, min_toys, max_toys;

  const auto & itmax = max_element( vei.cbegin(), vei.cend(), [](const auto & e1, const auto & e2) {return (e1.toys < e2.toys);});
  max_toys = itmax->toys;

  const auto & itmin  = min_element( vei.cbegin(), vei.cend(), [](const auto & e1, const auto & e2) {return (e1.toys < e2.toys);});
  min_toys = itmin->toys;

  total_toys = accumulate( vei.cbegin(), vei.cend(), 0,  [](int t, const auto & ei) -> int { return t +ei.toys;});

  for (const auto & ei : vei)
    cout << ei.to_string() << endl;

  cout << "total number of created toys " << total_toys << endl;
  cout << "maximum number of created toys " << max_toys << endl;
  cout << "minimum number of created toys " << min_toys << endl;
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.180712.1 | Last Updated 7 Dec 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100