Thread: C++ Question

Page 1 of 2
1
2
LastLast
  1. #1
    Deleted

    C++ Question

    I'm aware that there is google and several websites that might be more appropiate for asking questions on coding but going to post it here as I'm aware that some of you have experience with coding languages and I feel a bit more comfortable posting it here.

    I'm currently getting a headache over this C++ program I'm trying to make: Read a set of exam marks between 0 and 100 and create a histogram that shows how many marks in each decades (0-10, 11-20, 21-30...).

    The histogram should look like:

    00-10 | *
    11-20 | ****
    21-30 | *******

    It carries on up until 91-100.


    The part that is giving me a headache is defining the array called values and I haven't found a way to define it without causing more errors in the coding (Using Visual Studio 2010) and the part where I'm trying to get the output that puts it into a histogram.

    The code I have gotten so far:
    The words in italics are not actually written in the code but there to show where I'm getting headaches from.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    
    int main()
    {
    	int i, file;
    	
    	
    	ifstream infile("marks1.txt");
    	if (!infile)
    	{
    		cout << "Cant open file";
    		exit(EXIT_FAILURE);
    	}
    		for (i=0;i<file;i++)
    	{
    		infile >> values[i];      <---- Error shown with values (undefined)
    	}
    
    	
    	int marks, x, hold;
    	int decades [10] = {0-10, 11-20, 21-30, 31-40, 41-50, 51-60, 61-70, 71-80, 81-90, 91-100};
    	int count[10]; // Contains for each section, the occurence number.                                  <----- Is this the correct way?
    	 
    
    	for ( x = 0; x <= values.size(); x++)      <---- Error shown with values (undefined)
    	{
    		if(values[x] <= 10)
    			count[0]++;
    		else if(values[x] <=20)
    			count[1]++;
    		else if (values[x] <=30)
    			count[2]++;
    		else if (values[x] <=40)
    			count[3]++;
    		else if (values[x] <=50)
    			count[4]++;
    		else if (values[x] <=60)
    			count[5]++;
    		else if (values[x] <=70)
    			count[6]++;
    		else if (values[x] <=80)
    			count[7]++;
    		else if (values[x] <=90)
    			count[8]++;
    		else if (values[x] <=100)
    			count[9]++;
    	}
    
    	cout << "00-10 | " << count[0] * "*";    <----- Error shown with "*" (must be arithmetic or enum type) (For all cout)
    	cout << "11-20 | " << count[1] * "*";    
    	cout << "21-30 | " << count[2] * "*";
    	cout << "31-40 | " << count[3] * "*";
    	cout << "41-50 | " << count[4] * "*";
    	cout << "51-60 | " << count[5] * "*";
    	cout << "61-70 | " << count[6] * "*";
    	cout << "71-80 | " << count[7] * "*";
    	cout << "81-90 | " << count[8] * "*";
    	cout << "91-100| " << count[9] * "*";
    }

    I appreciate suggestions and advice offered as I only recently started learning C++. Bonus points if you can suggest a way (or point me in the right direction) to changing the histogram so it's vertical bars/stars going up the page. Apologies if this would be better asked in a different forum.

  2. #2
    Deleted
    well for starters i see you have a variable called: values
    But you havent defined it anywhere, fixed that for starters, else you have no way to use the peice
    infile>>values[i];

    and second if you plan to use int i in more places than just one for-loop then make sure you do that instead of just declaring one for-loop

    (saves some space to write it like this if you're just going to use it once:

    for(int i=0;i<file;i++);

    Just some minor tips, i actually did programs like this not long ago, just transfered to the Graphical library.

  3. #3
    Deleted
    Can I ask where and how to define it? I've tried defining it but it gives me other errors so I assume I must be doing it wrong. Would I define it earlier as

    int i, file;
    int values [];

    ifstream infile("marks1.txt");
    if (!infile)

    Wouldn't I need to give a number like int values [25] for the length of array?

  4. #4
    I believe you need to initialize the array with a size. leaving it blank would require you to populate the array when you define it.

    ie. values [] = {value1, value2, value3, value 4, .... }

    you need to run a similar for loop prior to get the upper size boundary for the array and then define it.
    Last edited by Tradewind; 2012-03-30 at 07:50 PM.

  5. #5
    Deleted
    you cant initiate a int_arr without an set value, unless you do as said above, same thing goes for char_arr.

  6. #6
    That is correct, you would need to declare the array first and give it a size. However, I don't think that would suit your program very well unless you know beforehand how many 'values' will be read into the program. (I assume it could be any number of values.) In this case, you would probably need to use an array of pointers and dynamically create/store the values into the array, which again I assume is a bit above your level of knowledge at this point in your studies. It might make more sense to use a List or Vector or some kind of ADT that can handle the values for you and pass them along there.

    BUT....IMO, you can save yourself the headache of needing to use the array by bypassing the need to store the values in memory. You can simply count each value as you read it in from the file. That way, you don't need to worry about these initialization errors, and you have more control of the variables within your program.

    Did you need to store these 'values' for some other function of your program?
    Last edited by gk22; 2012-03-30 at 08:06 PM.

  7. #7
    Do you know the number of lines in the file? If so you can cheat and do int values[# lines in file];

    if not read in the file and count the end line characters and use that to initialize your array, or skip the array and read the file line by line and just count it in your decades.
    Quote Originally Posted by Elrandir View Post
    My starfall brings all the mobs to the yard.
    Laurellen - Druid Smiteyou - lol holy dps

  8. #8
    ok, this is the whole code working:

    Ask here if you need more explanations or if you got any questions on anything

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    
    int main()
    {
    	int i, file;
    	const int MAX_SIZE=500;
    	int values[MAX_SIZE]; 
    	int size = 0;
    	
    	ifstream infile("marks1.txt");
    	if (!infile)
    	{
    		cout << "Cant open file";
    		exit(EXIT_FAILURE);
    	} 
    	while (!infile.eof())
    	{
    		infile >> values[size];     
    		size++;		
    	}
    
    	
    	int marks, x, hold;
    	int count[10]; // Contains for each section, the occurence number.                         
    	for (int i = 0; i < 10; i++)
    		count[i] = 0;	 
    
    	for ( x = 0; x < size; x++)     
    	{
    		count[(int)values[x]/10]++;		
    	}
    
    	for (int i = 0; i < 10; i++)
    	{
    		cout << i*10 << "-" << (i*10 + 9) << " | ";
    		for (int j = 0; j < count[i]; j++)
    		{
    			cout << "*";
    		}
    		cout << "\n";
    	}
    
    }
    "Blizzard is not incompetent or stupid and they are not intentionally screwing you over"

  9. #9
    This is just something I did for you while I was bored at work. Don't have Visual Studio installed on this particular machine I'm on right now, so I can't test it for you. But you should be able to understand the general concept. It's also your job to properly order where your variable initializations are, and comment your code. Not gunna do your assignment for you. :P Just giving you a push in the right direction.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    const int LIMITS = 200;
    
    int main()
    {
    	int i, file;
    	int values[LIMITS];
    	
    	
    	ifstream infile("marks1.txt");
    	if (!infile)
    	{
    		cout << "Can't open file";
    		exit(EXIT_FAILURE);
    	}
    		for (i=0;i<file;i++)
    	{
    		infile >> values[i];
    	}
    
    	
    	int marks, hold;
    	int decades [10] = {0-10, 11-20, 21-30, 31-40, 41-50, 51-60, 61-70, 71-80, 81-90, 91-100};
    	int count[10];
    	 
    
    	for ( int x = 0; x <= values.size(); x++)
    	{
    		if(values[x] <= 10)
    			count[0]++;
    		else if(values[x] <=20)
    			count[1]++;
    		else if (values[x] <=30)
    			count[2]++;
    		else if (values[x] <=40)
    			count[3]++;
    		else if (values[x] <=50)
    			count[4]++;
    		else if (values[x] <=60)
    			count[5]++;
    		else if (values[x] <=70)
    			count[6]++;
    		else if (values[x] <=80)
    			count[7]++;
    		else if (values[x] <=90)
    			count[8]++;
    		else if (values[x] <=100)
    			count[9]++;
    	}
    	
    	for(int x = 0; x < 10; x++) 
    	{
    		cout << decades[x] << " | ";
    			
    		for(int y = 0; y < count[y] < y++)
    		{
    			cout << "*";
    		}
    		cout << endl;
    	}
    }
    EDIT: This is also entirely based off your own base code. Marooned took it a step further than I did and overhauled it completely to give you some decently clean code to work with. But if this is for an introductory C++ course (which it looks like it is), I don't think Marooned's code will be very believable for someone who's new at C++ lol.
    Last edited by Mawaru; 2012-03-30 at 08:21 PM.

  10. #10
    I haven't done c++ in years and don't have anything installed to test with soooo if it doesn't work don't shoot me =P

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	//int i, file; <--not needed anymore
    	int value;
    	//int marks, hold;  <-- not sure what this is for
    	// int decades [10] = {0-10, 11-20, 21-30, 31-40, 41-50, 51-60, 61-70, 71-80, 81-90, 91-100};  <-- also ummm i don't think that this right either.  
    	int count[10];  //might need ={0,0,0,0,0,0,0,0,0,0};
    	 
    
            ifstream infile("marks1.txt");
    	if (!infile)
    	{
    		cout << "Can't open file";
    		exit(EXIT_FAILURE);
    	}
            while (!infile.eof())
    	{
    		infile >> value;  //Read a value
    		//count it
                    count[(int)ceil(value/10)]++;
    	}
    	
    	for(int x = 0; x < 10; x++) 
    	{
    		cout << (x*10)+1 << "-" << (x+1)*10 << " | ";
    			
    		for(int y = 0; y < count[y] < y++)
    		{
    			cout << "*";
    		}
    		cout << endl;
    	}
    }
    Last edited by gamingmuscle; 2012-03-30 at 08:33 PM.
    Quote Originally Posted by Elrandir View Post
    My starfall brings all the mobs to the yard.
    Laurellen - Druid Smiteyou - lol holy dps

  11. #11
    Deleted
    Thanks for replying guys, just having my dinner at the moment so I'll take a closer look in a few moments. Again, thanks for helping out.

    ---------- Post added 2012-03-30 at 11:10 PM ----------

    I've come across another problem in which it tells me that "expression must have class type" for:

    for ( int x = 0; x <= values.size(); x++)

  12. #12
    values->size()

    not

    values.size()

    I do believe.

  13. #13
    Deleted
    I've tried that and it comes up with a different error: expression must have pointer-to-class type

  14. #14
    Quote Originally Posted by Scotai View Post
    I've tried that and it comes up with a different error: expression must have pointer-to-class type
    In C for normal arrays there is no size or length function, best you can do is something like this:

    int len=sizeof(array)/sizeof(int);

    thats why i keep track of size in a variable, try my code i ran it in VS2010 without errors
    "Blizzard is not incompetent or stupid and they are not intentionally screwing you over"

  15. #15
    Quote Originally Posted by Scotai View Post
    I've tried that and it comes up with a different error: expression must have pointer-to-class type
    Yeah, sorry. It's because .size is a string or vector function, not for arrays.

    what marooned posted, it calculates the bitsize of the array and divides it by the value size to get the number of elements (sort of...).

  16. #16
    Deleted
    I tried your code Marooned aswell as Mawaru (focussed on Mawaru's because it was a bit easier to understand). For me it opens the black box but closes straight away, dunno if that's because I need cout >> hold; or because it can't find the file.

  17. #17
    press Ctrl+F5 instead of F5
    "Blizzard is not incompetent or stupid and they are not intentionally screwing you over"

  18. #18
    Deleted
    Oooh, can't find the file.

  19. #19
    To use values.size() he has to use some sort of template or class for values like std::vector for example. In that case defining values would look like
    std::vector<int> values; // this is dynamic array right now so it can be any size you want

    To add some values into that array you have to read from file to some int varibale first. And then use values.push_back(value) where value stores int from file.

  20. #20
    give it a path like c:\\your-directory\\filename
    "Blizzard is not incompetent or stupid and they are not intentionally screwing you over"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •