Hi there all,

Could anybody please help me, as to why my program is not adding the right quantity's?

// Question 2a
#include <iostream>
using namespace std;

int main ( )
{
    const int NR_SESSIONS = 3;
    int nrAttend;
    int votesForS, votesForR, votesForO;
    char vote;
    
// Initialise totals
    votesForS = 0;
    votesForR = 0;
    votesForO = 0;

// Loop over voting stations
    for (int i = 1; i <= NR_SESSIONS; i++)
    {
        cout << "SESSIONS " << i << endl;
        cout << "How may pupils attended this session? " << endl;
        cin >> nrAttend;
        
// Loop over attendants  

    cout << "First: Which character would you like to vote for? ";
    cin >> vote;
    for (int i = 2; i <= nrAttend; i++)
    
    switch (vote)
    {
        case 'S':
        votesForS++; break;
        case 'R':
        votesForR++; break;
        default:
        votesForO++;
        
     cout << "Next: For whom do you vote? ";
     cin >> vote;
    }
}

// Displays results
    cout << endl << endl;
    cout << "Votes for Scarlet:  " 
         << votesForS << endl;
    cout << "Votes for Rhett:  " 
         << votesForR << endl;
    cout << "Votes for Other:  " 
         << votesForO << endl;
    
    return 0;
}

Recommended Answers

All 12 Replies

Line 28:
1) use a different loop counter name because it is hiding the loop counter declared on line 18.
2) why does that loop begin at 2 instead of 0, such as like this? for(int j = 0; j < nrAttend; ++j)

Hi Ancient Dragon,

Thank you for the response, I changed what you suggested but now it's not adding up the characters ie. R, S or O?

i see something strange about your block, about the placement of brackets..

Your indentation goes a bit off from line 26 onwards (but that is more of an aesthetic thing which affects the readability of your code, it doesn't affect the code in any other way..The compiler doesn't care about whitespace or readability of code..As long as the syntax is correct the compiler is happy!)

Also your switch statement is incorrect. You need to move your cout and cin statements outside the switch statement. As it stands they'll only get called if the user enters something other than S or R.

Once you've moved those out of the switch, you'll also need to add an opening brace under your 2nd 'for' loop (don't forget to add a closing brace further down!). You should declare your 2nd for loop as Ancient Dragon has suggested.

In other words, your 2nd for loop should look like this:

for (int j = 0; j < nrAttend; ++j)
    {
    	switch (vote)
    	{
        	case 'S':
        		votesForS++; 
			break;
        	case 'R':
        		votesForR++; 
			break;
        	default:
        		votesForO++;
     	}   
     	cout << "Next: For whom do you vote? ";
     	cin >> vote;
    }

TECHNICAL NOTE: In Ancient Dragons code (and in my code above), we've used the prefix increment operator (++j) instead of the postfix operator (j++) in the increment section of the for loop. This is an old C/C++ optimisation trick.

All you need to know is that the prefix operator is a teeny tiny bit quicker than the postfix operator (I could explain further, but I can't be arsed!).
If you were looping thousands of times, using the prefix incrementor could save quite a few machine ticks, but as you're only iterating a handful of times, it doesn't amount to a hill of beans, so the prefix or postfix operator could be used without affecting the speed of your program! heh heh!

Anyway, enough of that technical nonsense, back to the task in hand....with the indentation fixed and with your restructured for loop, your code should look something like this:

// Question 2a
#include <iostream>
using namespace std;

int main ( )
{
	const int NR_SESSIONS = 3;
	int nrAttend;
	int votesForS, votesForR, votesForO;
	char vote;
    
	// Initialise totals
	votesForS = 0;
	votesForR = 0;
	votesForO = 0;

	// Loop over voting stations
	for (int i = 1; i <= NR_SESSIONS; i++)
	{
		cout << "SESSIONS " << i << endl;
		cout << "How may pupils attended this session? " << endl;
		cin >> nrAttend;
        
		// Loop over attendants  
        	cout << "First: Which character would you like to vote for? ";
		cin >> vote;

		for (int j = 0; j < nrAttend; ++j)
		{
			switch (vote)
			{
			case 'S':
				votesForS++; 
				break;
			case 'R':
				votesForR++; 
				break;
			default:
				votesForO++;
			}
   
			cout << "Next: For whom do you vote? ";
			cin >> vote;
		}
	}

	// Displays results
	cout << endl << endl;
	cout << "Votes for Scarlet:  " << votesForS << endl;
	cout << "Votes for Rhett:  " << votesForR << endl;
	cout << "Votes for Other:  " << votesForO << endl;
    
	return 0;
}

Hey JasonHippy,

Thanks for all the info... I understand now where i went wrong.

Void Functions :'( What is it i'm doing wrong in this program?

// Assignment 2 Question 4c
#include <iostream>
using namespace std;

// The required function drawShape should be inserted here.

void drawShape(int nrR, char a, char b)
{
    for (int i = 1; i <= nrR; i++)
    {
      for (int j = 1; j <= (nrR - i + 1) ; j++)
        cout << b;
      for (int j = 1; j <= (2 * i - 1); i++)
        cout << a;
      for (int j = 1; j <= (nrR - i + 1) ; j++)
        cout << b;
        cout << endl;
    }
    
    for (int i = 1; i <= (2 * nrR + 1); i++)      // Bottom row
        cout << b;
    cout << endl;
}      

int main ( )
{
    int nr;
    char charV, charBack;
    
    cout << "Number of rows: ";
    cin >> nr;
    cout << "Character large V: ";
    cin >> charV;
    cout << "Background character: ";
    cin >> charBack;
 
    drawShape(nr, charV, charBack); 
    
    return 0;
}
for (int j = 1; j <= (2 * i - 1); i++)

Your bug is the reason why I do not use i and j for counters. ;) They look so much alike it is easy to mistype and not be able to see it. In the code above you increment i instead of j. x and y are easier to tell apart. Compare this with your code and put the bug back in to see if it is easier to find with different counter names:

void drawShape(int nrR, char a, char b)
{
    for (int x = 1; x <= nrR; x++)
    {
        for (int y = 1; y <= (nrR - x + 1) ; y++) cout << b;
        for (int y = 1; y <= (2 * x - 1); y++) cout << a;
        for (int y = 1; y <= (nrR - x + 1) ; y++) cout << b;
        cout << endl;
    }

    // Bottom row
    for (int x = 1; x <= (2 * nrR + 1); x++) cout << b;
    cout << endl;
}

Ha ha, Thanks Tommy... You guys make it sound so easy...........

You guys make it sound so easy...........

It only sounds easy because we have learned by making the same mistakes before. I still mistype counters all the time, so I know how to find it and prevent it. ;)

It only sounds easy because we have learned by making the same mistakes before. I still mistype counters all the time, so I know how to find it and prevent it. ;)

Exactly. One of the other typos I still make now and then is : if (a=1) versus if (a==1) Which can also give you a headache..

Exactly. One of the other typos I still make now and then is : if (a=1) versus if (a==1) Which can also give you a headache..

Yea, but if you write it like this: if(1 = a) your compiler will report it as an error, saving you much headache :P

Yea, but if you write it like this: if(1 = a) your compiler will report it as an error, saving you much headache :P

But when you write it like this: if (a = b) your compiler might not make a peep. I do not use this idiom because it only applies when one of the operands is a constant. If you can remember to swap the operands when one is a constant but take care to use == when both are variable, you can remember to use lint and not worry about it. :D

^^ Good point :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.