Hi Guys,

Im completely new to this website and quite new to programming,
Currently im doing Programming at college,

Due to some unforeseen circumstances i had to miss a few weeks at college,
Could someone please help me with a problem? I have been trying to figure it out on my own, but i missed some essential classes so i am quite lost,

I have been asked to create a code, Which is find the average of grades input for quizzes (This part is easy enough),

But what im stuck on is, The number of quizzes is a variable, there could be 3 quizzes or even 8 quizzes etc (Im sure you get my point).

What i am not sure, Is how to create a loop, that will allow me to input the grades according to what ever number the user inputs for the number of quizzes?

For Ex:

Enter number of quizzes: (User enters any number (6))
Quiz Grade 1:
etc etc
Quiz Grade 6:

How do i make a for loop or if switch command for this?

Any help is greatly appreciated!

Thanks!

Can you please specify your problem clearly

I agree with Rohan121212; you need to give more specifics about the program.

The answer to the question would depend on just how the end of the loop is indicated. If you are given a number of cases to enter before the loop begins, then it is fairly straightforward:

int quiz_count; 

    cout << "Number of quizzes to enter: ";
    cin >> quiz_count;
    cin.ignore();
    for (int i = 0; i < quiz_count; i++)
    {
        read_quiz();
    }

OTOH, if you don't have the number of quizzes ahead of time, a while() or do..while() loop would be better:

do 
    {
        read_quiz();

        char another_quiz;
        cout << "Enter another quiz (Y/N)? ";
        cin >> another_quiz;
        cin.ignore();
    } while (toupper(another_quiz) == 'Y');

Do either of these solve your problem?

commented: Helped me out ^_^ +1

Well I didn't get what you meant But according to what you have written: This should help.

#include<<iostream.h>> //If you have a latest compiler <<iostream>> will do
int main()
{
     int n; //no.of quizzes
     int sum=0, total;
     float avg; //Since average can have decimal values
     cout<<"Enter the number of quizzes"<<endl;
     cin>>n;
     int marks=new int[n]; //Here we dynamically assigned the size of the array. 

     for(int i=0;i<n;i++) /*What this basically do is, First value of i=0, check whether i<n, then runs the loop, after that value of i is incremented by 1*/

     { cout<<"Enter the mark of "<<i+1<<" Quiz"<<endl;
       cin>>marks[i]; // here we assign the values for each quiz ie. Marks.
     }
//Now we need a loop to find the sum of all the elements in the array
     for(int i;i<n;i++)
     { sum+=marks[i]} // Here all the marks of are added to the variable sum

     avg=sum/n;
     cout<<"average of the marks is"<<avg;
     cin.get();
     return 0;
}

I too am a beginner in c++ (17 years old). And i haven't compiled it. And i am pretty sure there can be some error. But what you wanted (LOOP) is pretty much clean here. Remaining must be easily rectified by you itself.
And i you have any trouble with any part of this program. Please let me know. I will clarify it....
Its just that i am too lazy to debug this again...

I agree with Rohan121212; you need to give more specifics about the program.

The answer to the question would depend on just how the end of the loop is indicated. If you are given a number of cases to enter before the loop begins, then it is fairly straightforward:

int quiz_count; 

    cout << "Number of quizzes to enter: ";
    cin >> quiz_count;
    cin.ignore();
    for (int i = 0; i < quiz_count; i++)
    {
        read_quiz();
    }

OTOH, if you don't have the number of quizzes ahead of time, a while() or do..while() loop would be better:

do 
    {
        read_quiz();

        char another_quiz;
        cout << "Enter another quiz (Y/N)? ";
        cin >> another_quiz;
        cin.ignore();
    } while (toupper(another_quiz) == 'Y');

Do either of these solve your problem?

For the OP's fairly specific question, I think there is enough information here.

According to the OP, the number of quizzes to enter is a pre-determined, and known, quantity which the program is given before the actual values are entered:

<snip>how to create a loop, that will allow me to input the grades according to what ever number the user inputs for the number of quizzes?

For Ex:

Enter number of quizzes: (User enters any number (6))
Quiz Grade 1:
etc etc
Quiz Grade 6:

So that is a non-issue. The bigger question is if anything else must be done with the data before the program ends. If so, then the OP must use some sort of storage/container that has a dynamically-determined capacity.

@OP:
At the most basic level, there are 2 different types of loops, determinate and indeterminate. A determinate loop runs for a pre-determined number of iterations then terminates, whereas an indeterminate loop runs until a certain event occurs such as a certain input or the state of a value changes in a certain way.

Additionally, a determinate loop can have either a constant or variable number of iterations; an indeterminate loop is always variable. What makes the difference between a variable determinate and an indeterminate loop is whether the number of iterations is known before the loop executes or not.

A determinate loop is best implemented using the for construct; you can also use the while or do-while constructs, but they aren't as "clean". The while and do-while are better suited for indeterminate loops.

This is a constant determinate loop:

#include <iostream>

const int MAX_VALUE = 5;

int main() {
  std::cout << "This program counts up to " << MAX_VALUE << std::endl;

  //begin constant determinate loop
  for (int i = 0; i <= MAX_VALUE; ++i) {
    std::cout << i << std::endl;
  } //end loop

  std::cin.get();

  return 0;
}

this program is a variable determinate loop:

#include <iostream>

int main() {
  int userCount = 0;
  std::cout << "What would you like me to count to?" << std::endl;

  std::cin >> userCount;
  std::cin.ignore();

  //begin variable determinate loop
  for (int i = 0; i <= userCount; ++i) {
    std::cout << i << std::endl;
  } //end loop

  std::cin.get();

  return 0;
}

this program is an indeterminate loop:

#include <iostream>

int main() {
  int inputValue = 0;

  //begin indeterminate loop
  do {
    std::cout << "Enter a positive integer value (negative to end): ";
    std::cin >> inputValue;
    std::cin.ignore();
  } while (inputValue >= 0);  //end loop

  std::cout << "Negative value detected, ending program..." << std::endl;

  std::cin.get();

  return 0;
}

You will want to model your program after the variable determinate loop style...

commented: Very Helpful! Thanks Friend! +1

Hey guys!

Wow thanks so much for your replies!
I really didnt think anyone was going to help me, Thats great!

Sorry if my explanation was not clear enough, I tried to give as much detail as possible, without blabbering on too much.

Right well in regards with the program,
Basically,

The user has to input a number,
That number will be the number of quizzes,

So the program will ask for how many Quiz results will be input,
Eg: Enter number of Quizzes: 6 <---For example i enter 6 (This input could be any number)

The program will then ask me for each individual quiz result,
Quiz Grade 1:
Quiz Grade 2:
Quiz Grade 3:
Quiz Grade 4:
Quiz Grade 5:
Quiz Grade 6:

Then i will have to add the total score together and divide by the number of quizzes input (6 in this case) to get the average score,

Then if the result is greater than 75% the program will show : Pass
If else, its below it will show : Fail,

My only concern was how to get a loop command,
That gave each Quiz Grade i input its own variable, so that i can find the average after?
I really cant get my head round it at the moment >"<...

I hope that makes more sense?

Thanks for all your help guys!
Im not at home at the moment so i cant play with the code, But when i get home i will try what you have suggested,

Any further help is greatly appreciated ^_^

Making a standard Loop and counting prog I can do,

The only thing im confused about,
Is how to loop it so that i can input a variable to each one,
So that i can then add all the inputs together to get a total and an average score of the grades input?

I hope im making more sense here ^_^ eheh

Well I didn't get what you meant But according to what you have written: This should help.

#include<<iostream.h>> //If you have a latest compiler <<iostream>> will do
int main()
{
     int n; //no.of quizzes
     int sum=0, total;
     float avg; //Since average can have decimal values
     cout<<"Enter the number of quizzes"<<endl;
     cin>>n;
     int marks=new int[n]; //Here we dynamically assigned the size of the array. 

     for(int i=0;i<n;i++) /*What this basically do is, First value of i=0, check whether i<n, then runs the loop, after that value of i is incremented by 1*/

     { cout<<"Enter the mark of "<<i+1<<" Quiz"<<endl;
       cin>>marks[i]; // here we assign the values for each quiz ie. Marks.
     }
//Now we need a loop to find the sum of all the elements in the array
     for(int i;i<n;i++)
     { sum+=marks[i]} // Here all the marks of are added to the variable sum

     avg=sum/n;
     cout<<"average of the marks is"<<avg;
     cin.get();
     return 0;
}

I too am a beginner in c++ (17 years old). And i haven't compiled it. And i am pretty sure there can be some error. But what you wanted (LOOP) is pretty much clean here. Remaining must be easily rectified by you itself.
And i you have any trouble with any part of this program. Please let me know. I will clarify it....
Its just that i am too lazy to debug this again...

I think you hit the nail straight on the head here Capn!
When i go home i will have a little play, I hope i sort it out correctly! =P

Thanks again ^_^

I agree with Rohan121212; you need to give more specifics about the program.

The answer to the question would depend on just how the end of the loop is indicated. If you are given a number of cases to enter before the loop begins, then it is fairly straightforward:

int quiz_count; 

    cout << "Number of quizzes to enter: ";
    cin >> quiz_count;
    cin.ignore();
    for (int i = 0; i < quiz_count; i++)
    {
        read_quiz();
    }

OTOH, if you don't have the number of quizzes ahead of time, a while() or do..while() loop would be better:

do 
    {
        read_quiz();

        char another_quiz;
        cout << "Enter another quiz (Y/N)? ";
        cin >> another_quiz;
        cin.ignore();
    } while (toupper(another_quiz) == 'Y');

Do either of these solve your problem?

Ahhh thanks alot!
I will also try this one out,
Im at home now, So i will give it a play =P

Thanks again =D,
Can you suggest any good websites so i can do some tutorials on my own?
My skills are basic only,

Many thanks!

Please do add reputation or vote points to anyone who really helped you, and when you post something here, you can expect a lot of replies that really helps, because people are here just for the one reason: Improve Programming skills Buy correction or by posting their codes. And please don't forget to Tag your post as Solved, if it is solved once in for all.......

commented: Great Help on my forum post! +1

You do not need to set a variable for each of the quizes
When you assign values to an array it automatically generates said amount of variables
Here I HAVE Created the Program you wanted
Reply if you found this answer helpful
I have commented alongside so that you can understand

#include<iostream>
using namespace std;
int main()
{
    int i;//for setting the loop
    int n;//for the number of quizes
    int quiz[100];//Array
    int sum=0;//for the total
    float average;//for the average
    float result;
    cout <<"Enter the number of quizes (maximum-100)";//If you want to increase it more than 100 then just increase the array value
    cin >>n;//the number of quizes is taken
    for(i=1;i<=n;i++)//Here I have set a loop which will take the values entered by the user and put it into the array
    {
                     cout<<"Enter the result of quiz "<<i<<"(OUT OF 100)"<<'\n';//I have given a base value 100 that means that every quiz was of 100 marks (Its easy now to get the percentage)
                     cin>>quiz[i];
    }
    for(i=1;i<=n;i++)//This array is to get the sum of all the marks entered by you
    {
                     sum+=quiz[i];
    }
    average=sum/n;
    cout<<"Total Marks - "<<sum<<'\n';
    cout <<"Average="<<average<<'\n';
    result=(sum/(n*100))*100;
    cout<<"Result -\n";
    if (result>75)cout<<"You have passed!!!!!!!!!!!!";
    else cout<<"You have failed\n";
    system("pause");
    return 0;
}
commented: Thx for the advice! Helpful person =D +1

Why do you have 2 loops when you just add to sum after line 16.
This can be easily done without the need for arrays.

Making a standard Loop and counting prog I can do,

The only thing im confused about,
Is how to loop it so that i can input a variable to each one,
So that i can then add all the inputs together to get a total and an average score of the grades input?

I hope im making more sense here ^_^ eheh

Are you at all familiar with vectors? The behavior you are describing makes this program a good candidate for them. See here.

You do not need to set a variable for each of the quizes
When you assign values to an array it automatically generates said amount of variables
Here I HAVE Created the Program you wanted
Reply if you found this answer helpful
I have commented alongside so that you can understand

#include<iostream>
using namespace std;
int main()
{
    int i;//for setting the loop
    int n;//for the number of quizes
    int quiz[100];//Array
    int sum=0;//for the total
    float average;//for the average
    float result;
    cout <<"Enter the number of quizes (maximum-100)";//If you want to increase it more than 100 then just increase the array value
    cin >>n;//the number of quizes is taken
    for(i=1;i<=n;i++)//Here I have set a loop which will take the values entered by the user and put it into the array
    {
                     cout<<"Enter the result of quiz "<<i<<"(OUT OF 100)"<<'\n';//I have given a base value 100 that means that every quiz was of 100 marks (Its easy now to get the percentage)
                     cin>>quiz[i];
    }
    for(i=1;i<=n;i++)//This array is to get the sum of all the marks entered by you
    {
                     sum+=quiz[i];
    }
    average=sum/n;
    cout<<"Total Marks - "<<sum<<'\n';
    cout <<"Average="<<average<<'\n';
    result=(sum/(n*100))*100;
    cout<<"Result -\n";
    if (result>75)cout<<"You have passed!!!!!!!!!!!!";
    else cout<<"You have failed\n";
    system("pause");
    return 0;
}

This is actually a pretty inefficient, and dangerous, solution because it relies on a lot of assumptions. It wastes memory and has the potential to easily cause seg faults, divide by zero errors, and more because there is no error catching.

Assuming the OP can use vectors, something like this would be more appropriate:

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::cin;
using std::endl;

typedef int dataType;
typedef std::vector<dataType> containerType;

int main() {
  //initialize local variables
  int valueCount = 0;     //accepts user input for the number of values to store
  containerType myValues; //storage container for the values
  float average = 0.0f;   //the average value

  //prompt the user for the number of values
  do {
    cout << "How many values would you like to enter (must be greater than 0): ";
    cin >> valueCount;
    cin.ignore();
  } while (valueCount <= 0);

  //input the required number of values
  for (int i = 0; i < valueCount; ++i) {
    //declare and initialize a local input variable
    dataType newValue = 0;

    //prompt the user for the new value
    cout << "Enter value " << (i+1) << ": ";
    cin >> newValue;
    cin.ignore();

    //add the new value to the container
    myValues.push_back(newValue);
  }

  //add up the values
  dataType sum = 0;
  for (containerType::size_type j = 0; j < myValues.size(); ++j) {
    //...
  }

  //calculate the average
  //...

  //output the results
  //...

  std::cin.get();

  return 0;
}

the person said he was a newbie and mentioned this as a array problem so i guess he is not comfortable with vectors

the person said he was a newbie and mentioned this as a array problem so i guess he is not comfortable with vectors

They also mentioned that they missed a couple weeks' worth of classes. It's possible that vectors were part of those classes.

It's simple enough to do it as an array, but they'll have to use dynamic allocation. I tend to shy away from this option when working with a newbie because it's generally considered an advanced topic that's covered late in a beginner class, if at all; it's not uncommon for it to be part of an intermediate/advanced class instead.

I see no mention in the original post concerning arrays.
Again with no error checking.
Maybe i'm reading this all wrong but what's wrong with

#include <iostream>

using namespace std;

int main()
{
    int quiznum;
    double total=0,result,average;
    cout<<"Enter amount of quizes: ";
    cin>>quiznum;
    
    for(int i=1;i<=quiznum;++i)
    {
            cout<<"Enter result: ";
            cin>>result;
            total+=result;
    }
    
    average=total/quiznum;
    
    cout<<"\nThe average is "<<average<<"\n";
    if(average>=75) cout<<"Pass\n";
    else cout<<"Fail\n";
    
    return 0;
}

I see no mention in the original post concerning arrays.
Again with no error checking.
Maybe i'm reading this all wrong but what's wrong with

#include <iostream>

using namespace std;

int main()
{
    int quiznum;
    double total=0,result,average;
    cout<<"Enter amount of quizes: ";
    cin>>quiznum;
    
    for(int i=1;i<=quiznum;++i)
    {
            cout<<"Enter result: ";
            cin>>result;
            total+=result;
    }
    
    average=total/quiznum;
    
    cout<<"\nThe average is "<<average<<"\n";
    if(average>=75) cout<<"Pass\n";
    else cout<<"Fail\n";
    
    return 0;
}

There is nothing wrong with that (other than not looking for Div. by 0, which you've acknowledged). But, later in the thread (this post), the OP mentioned that they need to store the values then perform the required calculation(s). So unless they're either not entirely sure what they need their self or are not being sufficiently clear, this pretty much implies the use of some sort of container, preferably either an array or a vector.

I see no mention in the original post concerning arrays.
Again with no error checking.
Maybe i'm reading this all wrong but what's wrong with

#include <iostream>

using namespace std;

int main()
{
    int quiznum;
    double total=0,result,average;
    cout<<"Enter amount of quizes: ";
    cin>>quiznum;
    
    for(int i=1;i<=quiznum;++i)
    {
            cout<<"Enter result: ";
            cin>>result;
            total+=result;
    }
    
    average=total/quiznum;
    
    cout<<"\nThe average is "<<average<<"\n";
    if(average>=75) cout<<"Pass\n";
    else cout<<"Fail\n";
    
    return 0;
}

Frogboy,
You pretty much got what i needed bang on, But hahaa,
Dude you did all the work for me lol >"<

Which i must say i do appreciate, I will play around this this,
I like it as its nice and simple and it does what i needed it to do,
Which was not something very advanced in the first place,

Im going to go back through now all the other code provided and have a play around with what people suggested,
So i can get a better understanding of this all....

How do i rate peoples help and then mark this thread as solved?

There is nothing wrong with that (other than not looking for Div. by 0, which you've acknowledged). But, later in the thread (this post), the OP mentioned that they need to store the values then perform the required calculation(s). So unless they're either not entirely sure what they need their self or are not being sufficiently clear, this pretty much implies the use of some sort of container, preferably either an array or a vector.

Hello fbody,

Thanks for your reply,
What was provided was nearly spot on to what i was looking for,
Sorry if i may described what i needed a bit wrong,

When i meant ti needed to "store" the values, I only meant for that instance,

The scenario would be,
The number of quizzes would be unknown, the user inputs this... Eg.... 3
The program will then ask for 3 Quiz grades,
Then add them up and get their average..

Then after that, Program will be ended and the job has been done,
Im not yet on arrays or vectors, That will be next term, well , next month even,

Can you suggest any good website that may have simple "exercises" that gradually get harder so i can improve my skills myself? As i believe thats the most important thing to becoming a good programmer,

Thanks again!

Frogboy,
You pretty much got what i needed bang on, But hahaa,
Dude you did all the work for me lol >"<

Which i must say i do appreciate, I will play around this this,
I like it as its nice and simple and it does what i needed it to do,
Which was not something very advanced in the first place,

Im going to go back through now all the other code provided and have a play around with what people suggested,
So i can get a better understanding of this all....

How do i rate peoples help and then mark this thread as solved?

There are rating arrows at the top-right of each post. After you click an arrow, it will offer you the opportunity to either add or subtract reputation points.

To mark the thread solved, there will be a link near the bottom of the thread display (not the overall page) that reads "Mark this thread as solved".

Hello fbody,

Thanks for your reply,
What was provided was nearly spot on to what i was looking for,
Sorry if i may described what i needed a bit wrong,

When i meant ti needed to "store" the values, I only meant for that instance,

The scenario would be,
The number of quizzes would be unknown, the user inputs this... Eg.... 3
The program will then ask for 3 Quiz grades,
Then add them up and get their average..

Then after that, Program will be ended and the job has been done,
Im not yet on arrays or vectors, That will be next term, well , next month even,

Can you suggest any good website that may have simple "exercises" that gradually get harder so i can improve my skills myself? As i believe thats the most important thing to becoming a good programmer,

Thanks again!

This is a site that has a decent tutorial. Otherwise, google "C++ Tutorials". Friendly warning: in general, video tutorials suck; if you find one, stay away.

Frogboy,
You pretty much got what i needed bang on, But hahaa,
Dude you did all the work for me lol >"<

VERY little work involved.

Are you at all familiar with vectors? The behavior you are describing makes this program a good candidate for them. See here.


This is actually a pretty inefficient, and dangerous, solution because it relies on a lot of assumptions. It wastes memory and has the potential to easily cause seg faults, divide by zero errors, and more because there is no error catching.

Assuming the OP can use vectors, something like this would be more appropriate:

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::cin;
using std::endl;

typedef int dataType;
typedef std::vector<dataType> containerType;

int main() {
  //initialize local variables
  int valueCount = 0;     //accepts user input for the number of values to store
  containerType myValues; //storage container for the values
  float average = 0.0f;   //the average value

  //prompt the user for the number of values
  do {
    cout << "How many values would you like to enter (must be greater than 0): ";
    cin >> valueCount;
    cin.ignore();
  } while (valueCount <= 0);

  //input the required number of values
  for (int i = 0; i < valueCount; ++i) {
    //declare and initialize a local input variable
    dataType newValue = 0;

    //prompt the user for the new value
    cout << "Enter value " << (i+1) << ": ";
    cin >> newValue;
    cin.ignore();

    //add the new value to the container
    myValues.push_back(newValue);
  }

  //add up the values
  dataType sum = 0;
  for (containerType::size_type j = 0; j < myValues.size(); ++j) {
    //...
  }

  //calculate the average
  //...

  //output the results
  //...

  std::cin.get();

  return 0;
}

Hi Fbody,

Once again ^_^

Thanks for submitting this code,
I am not on Vectors yet, But what i do appreciate is the code not being complete,
So i have to add my own "initiative" to it,

So i will play around with this, So i can learn more about how it works ^_^

Thanks for the great help!!

I agree with Rohan121212; you need to give more specifics about the program.

The answer to the question would depend on just how the end of the loop is indicated. If you are given a number of cases to enter before the loop begins, then it is fairly straightforward:

int quiz_count; 

    cout << "Number of quizzes to enter: ";
    cin >> quiz_count;
    cin.ignore();
    for (int i = 0; i < quiz_count; i++)
    {
        read_quiz();
    }

OTOH, if you don't have the number of quizzes ahead of time, a while() or do..while() loop would be better:

do 
    {
        read_quiz();

        char another_quiz;
        cout << "Enter another quiz (Y/N)? ";
        cin >> another_quiz;
        cin.ignore();
    } while (toupper(another_quiz) == 'Y');

Do either of these solve your problem?

Thanks for your help my friend,
They were not exactly what i needed,

But this will certainly come in handy for me in the future, I will save this code on my PC so i can play with it in the future!

Thanks again =D

There are rating arrows at the top-right of each post. After you click an arrow, it will offer you the opportunity to either add or subtract reputation points.

To mark the thread solved, there will be a link near the bottom of the thread display (not the overall page) that reads "Mark this thread as solved".


This is a site that has a decent tutorial. Otherwise, google "C++ Tutorials". Friendly warning: in general, video tutorials suck; if you find one, stay away.

Thanks for that Rohan ^_^
Added Rep points to those that helped, and thread now SOLVED!!
Wooo hooo i can sleep happily haha =D

I thought The Op can understand Dynamic Array...So i have added that part too in the program.......And can anybody tell me what the whole fuss is about?

I thought The Op can understand Dynamic Array...So i have added that part too in the program.......And can anybody tell me what the whole fuss is about?

Thanks for your help friend,
I got accused for not putting in enough effort in by someone on this post,
I was not asking for someone to write the code out for me,

Just a bit of help with that scenario,
I don't really have anyone else to ask,
Hence why i came to this forum,

Once again thanks to all that helped me with this,
I have now gained some knowledge that i can take away with me and to work on...

Given the fact that of all the code posted on this thread (and there is more than enough) none of it is yours then an accusation of lack of effort i think is more than justified.

commented: Was this really necessary? -3
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.