Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Errors when compiling TestP.cpp random numbers program.

$
0
0
Hey everyone, I am getting errors when I try to compile this program:

Implement a method countValue() that counts the number of times an item occurs in a linked list. Remember to use the STL <list>

int countValue(list<int> front ,const int item);

Generate 20 random numbers in the range of 0 to 4, and insert each number in the linked list. Output the list by using a method which you would call writeLinkedList which you would add to the ListP.cpp.

In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.

Remember that all the above is to be included in the file ListP.ccp (Check out the discussion on Assign4).

Output:
Run: 2 3 4 0 1 0 2 4 2 3 3 4 3 3 3 0 0 2 0 2
0 : 5, 1 : 1, 2 : 5, 3 : 6, 4 : 3

The errors I'm getting are associated with a const declaration that I'm having a fit figuring out.
1. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Line 7
2. error C2146: syntax error : missing ',' before identifier 'val' Line 7
3. error C2065: 'String' : undeclared identifier Line 12
4, error C2146: syntax error : missing ';' before identifier 'val' Line 12
5. error C2065: 'val' : undeclared identifier line 12
6, error C2664: 'writeLinkedList' : cannot convert parameter 2 from 'const char [2]' to 'const int' Line 25
7. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int Line 48
8. error C2146: syntax error : missing ',' before identifier 'val' Line 48

I am not sure how to declare the "void writeLinkedList(list<int> front, const String val);" in line 7. If I remove the 'const String val' from the 'void writeLinkedList(list<int> front, const String val);' ,
the program compiles correctly, but my instructor requires it to be in program. I have looked at this until I am batty...


Here is my code:
#include <list>
#include <cstdlib>
#include <iostream>
using namespace std;

int countValue(list<int> front ,const int item);
void writeLinkedList(list<int> front, const String val);

int main()
{
	list<int> front;
	String val=" ";
	int listCount;
	int count=0;
	cout << "Enter the size of the list: ";
	cin >> listCount;

	for (int i = 1; i <= listCount; i++)
	{
		front.push_back(rand()%5);
	}
	cout << "Original List of Values:    "  << endl;
	cout << endl;
	
	writeLinkedList(front," ");
	for(int j=0;j<5;++j)
	{
		count=countValue(front,j);
		cout << j << " : " << countValue (front,j) << ", ";
	}
	cout << endl;
	system("pause");
	
}
int countValue(list<int> front, const int item)
{
	int count=0;
	list<int>::iterator i;
	for(i=front.begin(); i != front.end(); ++i)
	{
		if(item==*i)
			count=count+1;
	}
	return count;

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>