LynnisaMystery
LynnisaMystery

Reputation: 9

How to loop certain aspects of a program

I'm trying to loop the assignment scores and totals based on whatever the user inputs in for n. I have been searching around and just hitting duds, which is what the whole deal with the int i variable is at the moment. I can't get things to loop properly and right now this doesn't even compile because of that pesky i I have in there. You take out that i and the program runs fine with the exception that nothing becomes of the n input.

/**
File: project_2_14.cpp
Description: Write a program that calculates the total grade for N classroom exercises as a percentage. The user should input the value for N followed by each of the N scores and totals. Calculate the overall percentage (sum of the total points earned divided by the total points possible.) and output it as a percentage. Sample input and output is shown below.
Created: Friday, September 11th, 2015
Author: 
email: 
*/

#include<iostream>
#include<vector>

using namespace std;

int main() 
{

    float s;  // assignment score
    float t;  // total points worth
    float p;  // percentage     
    int n = 0;

    //input the number of assignments

    cout << "How many assignments are there? " << endl;
    cin >> n;

        for (int i=0; i < =n; i++)
        {


    //input the total points earned for assignment
    cout << "What is the score earned for this assignment? ";
    cin >> s;


    //input the number of points assignment is worth
    cout << "How many points was the assignment worth? ";
    cin >> t;

    //calculate percentage
    p = (s / t)*100;
        }


    //output score

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "Total score: " << p << "%"<< endl;

    return 0;
}

Upvotes: 1

Views: 1557

Answers (1)

Bruno Oliveira
Bruno Oliveira

Reputation: 745

You should remove the int n = 0; and it should be simply int n.

and you should use <=, without any spaces between both characters.

EDIT: As you can see here: http://ideone.com/SeNum8 it already loops correctly :)

#include<iostream>
#include<vector>

using namespace std;

int main() 
{

    float s;  // assignment score
    float t;  // total points worth
    float p;  // percentage     
    int n;

    //input the number of assignments

    cout << "How many assignments are there? " << endl;
    cin >> n;

        for (int i=0; i <=n; i++)
        {


    //input the total points earned for assignment
    cout << "What is the score earned for this assignment? ";
    cin >> s;


    //input the number of points assignment is worth
    cout << "How many points was the assignment worth? ";
    cin >> t;

    //calculate percentage
    p = (s / t)*100;
        }


    //output score

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "Total score: " << p << "%"<< endl;

    return 0;
}

Upvotes: 1

Related Questions