how to insert a loop input into my code [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
So here is my code
#include<iostream>
#include<fstream>
using namespace std;
int main() {
int program = 0;
int score = 0;
cout << "Enter the number of assignments that were graded. " << endl;
cin >> program;
for (int i=1; i <= program; i++)
cout << "Enter the score for assignment #" << i <<": ";
}
Basically I am trying to find a way that would allow the user to input their score along with the cout << Enter the score for assignment #"
Basically I would want the compilier to do this (example):
Enter the number of assignments that were graded: 3
Enter the score for assignment #1: 100
Enter the score for assignment #2: 75
Enter the score for assignment #3: 82
(and so, and so on.)
I really have no idea how I am suppose to put the input along the same loop that I created.
is there anyone that can help me out. what code can I use that would allow unlimited loops of inputs along the same line as Enter the score for assignment #x: (input)

You used incorrect syntax for include directives. Instead of
include iostream
include fstream
there must be
#include <iostream>
#include <fstream>
The program could look the following way
#include <iostream>
#include <vector>
int main()
{
unsigned int program = 0;
std::cout << "Enter the number of assignments that were graded: ";
cin >> program;
std::vector<unsigned int> scores( program );
for ( unsigned int i = 0; i < program; i++ )
{
std::cout << "Enter the score for assignment # " << i + 1 << ':';
std::cin >> scores[i];
}
// ...
Instead of type unsigned int you can use type size_t

Basically, you need to create a block of code in your for loop:
vector<int> grades;
for (int i = 0; i < program; ++i )
{
int temp_grade = 0;
cout << "Enter the score for assignment #" << i <<": "; cin >> temp_grade;
grades.push_back( temp_grade );
}
When you want to use some of the vectors items, you can use either subscripting or iterators.
For printing the grades via iterators:
for(vector<int>::iterator it = grades.cbegin(); it != grades.cend(); ++it)
cout << *it << " ";

I really have no idea how I am suppose to put the input along the same loop
Put another input inside the for loop:
for (int i=1; i <= program; i++) {
cout << "Enter the score for assignment # " << i <<": "<< endl;
int score;
cin >> score;
// ...
}
Writing loops or control flow (if,else) statements like this
for (int i=1; i <= program; i++)
statement();
is dangerous BTW. It's always better to use the braces {}.

Related

Writing program better [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
im really new to programming and really want some help:). I have made a program that finds the average out of 5 numbers. I was wondering i i could get some help to make this program more efficient. Im using c++.
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
int num4;
int num5;
int Anum;
int AAnum;
cout << "pick your first number";
cin >> num1;
cout << "pick your second numer";
cin >> num2;
cout << "pick you third number";
cin >> num3;
cout << "pick your forth number";
cin >> num4;
cout << "pick you fifth number";
cin >> num5;
Anum = num1 + num2 + num3 + num4 + num5;
AAnum = Anum / 5;
cout << "the average number is " << AAnum;
return 0;
}
Welcome to programming! How about something like this?
#include <iostream>
using namespace std;
int main() {
double average = 0.0;
const int num_to_average = 5;
for (int i = 0; i < num_to_average; ++i)
{
cout << "Pick number " << i << ": ";
double num;
cin >> num;
average += num;
}
average /= num_to_average;
cout << "the average number is " << average << end;
return 0;
That way the loop does all the heavy lifting for you so instead of 5 separate variables, you can add them all into a single variable (and then computes the average at the end).
Edit: included #tadman's double instead of float suggestion.
The runtime for this program is going to be as good as it's going to get. Each statement will be executed in O(1) time and the runtime performance probably isn't going to improve unless you change the way you prompt for the numbers (i.e. "Enter 5 numbers separated by spaces" and then do cin >> num1 >> num2 >> ...)
Now if you want a better way to write your algorithm, try an array.
At the top:
#define NUMBEROFNUMBERS 5
int inputArray[NUMBEROFNUMBERS];
for(int i = 0; i < NUMBEROFNUMBERS; i++) {
cout << "Pick #" << i + 1 << ": ";
cin >> inputArray[i];
}
And then calculate your average by looping through the array and outputting the result.
It's a short and elegant way to do that if you exactly know the elements of your array.
#include<iostream>
int main()
{
double result = 0;
const double myFirstArray[5] = { 5, 10, 22, 44, 5 };
for (int i = 0; i < std::size(myFirstArray); i++) {
result+=myFirstArray[i];
}
std::cout << "Average: " <<result/ std::size(myFirstArray);
return 0;
}

How can I program something that asks two integers from the user and displays it in the following sequence in C++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm a kind of beginner in C++ and need little help here.
How can I create a C++ program which takes two integers from the user and It displays the sequence in this format using Nested Loops:
Enter the starting integer: 11
Enter the ending integer: 18
(11,11)(11,12)(11,13)(11,14)(11,15)(11,16)(11,17)(11,18)
(12,12)(12,13)(12,14)(12,15)(12,16)(12,17)
(13,13)(13,14)(13,15)(13,16)
(14,14)(14,15)
or
Enter the starting integer: 1
Enter the ending integer: 5
(1,1)(1,2)(1,3)(1,4)(1,5)
(2,2)(2,3)(2,4)
(3,3)
I wrote the code something like this before:
int startingval;
cout << "Enter starting integer: ";
cin >> startingval;
int endingval;
cout << "Enter Ending integer: ";
cin >> endingval;
int looptime;
looptime = endingval;
endingval = startingval;
for (int i = 0; i < startingval; i++)
{
cout << "(" << startingval << ", " << endingval << ")";
endingval++;
if (endingval == looptime + 1)
{
i = startingval;
}
}
return 0;
But, This is not what I need. Please Help me :)
first, you can't use any tags except that's in your question.
the only difference between the sequence you want and the normal nested loop between all elements in the array is the ending point is decreesing
I suggest you try the following code:
int start;
cout << "Enter starting integer: ";
cin >> start;
int end;
cout << "Enter Ending integer: ";
cin >> end;
for(int i=start;i<=end;i++){
for(int j=i;j<=end;j++)
cout << "(" << i << ", " << j << ")";
cout<<endl;
end--;
}
I hope be useful, Let me know if there is a problem.

for loop not incrementing through table of array C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've tested my array separately to make sure it is working and holding all the values and it is but for some reason when I run it through my for loop it just prints out 10.95 in 3 rows and 3 columns I don't understand why it isn't pull the rest of the values from my table.
Here is the assignment:
Write, compile, and run a C++ program to input the following values into an array named prices: 10.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, and 17.59 After the data has been entered, have your program display the values in 3 rows and 3 columns.
Here is the code I have written:
#include <iostream>
#include <iomanip>
using namespace std;
const int COLS = 3;
const int ROWS = 3;
int main()
{
const int num_items = 9;
float prices[num_items];
cout << "Enter the prices of your " << num_items << " items: ";
cin >> prices[0];
cin >> prices[1];
cin >> prices[2];
cin >> prices[3];
cin >> prices[4];
cin >> prices[5];
cin >> prices[6];
cin >> prices[7];
cin >> prices[8];
float table[ROWS][COLS] = {{prices[0], prices[1], prices[2]},
{prices[3], prices[4], prices[5]},
{prices[6], prices[7], prices[8]}};
cout << "The prices you have entered are:\n";
for (int x = 0; x < ROWS; x++)
{
for (int y = 0; y < COLS; y++)
{
cout << setw(6) << table[ROWS][COLS] << " ";
}
cout << endl;
}
return0;
}
cout << setw(6) << table[ROWS][COLS] << " ";
should be
cout << setw(6) << table[x][y] << " ";

I need to calculate each students average grade [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
/*This code creates two text files : Mokiniai.txt and Vidurkiai.txt
In Mokiniai.txt there are stored each students grades, in Vidurkiai there
should be calculated each students average grade*/
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n, k, isvisopazymiu, i, pazymiai;
double vidurkis; //average grade
ofstream myfile;
myfile.open("Mokiniai.txt");
cout << "parasykite kiek mokiniu yra" << endl; cin >> n; //how many students
myfile << n << endl;
cout << "parasykite kiek yra mokiniu pazymiu"; cin >> isvisopazymiu; // how many grades one student has
for (k = 1; k <= n; k++) {
for (i = 1; i <= isvisopazymiu; i++) {
cout << "Parasykite kokie yra mokiniu pazymiai "; cin >> pazymiai; // what are students grades
myfile << " " << pazymiai;
}
myfile << endl;
}
myfile.close();
myfile.open("Vidurkiai.txt"); //trying to calculate average students grades on different file
for (k = 1; k <= n; k++) {
vidurkis = pazymiai / isvisopazymiu; //calculating students grades
myfile << k << " " << vidurkis << endl;
}
myfile.close();
return 0;
}
My problem is that: There is something wrong in vidurkiai.txt file, but I don't know what is wrong.
For example: first students grades are : 7 8 9 7 8, and average grade should be 7,8, but after coding it, in vidurkiai.txt file it shows, that average grade is 1.
The reason why it does not show the grades as expected is because you are not adding up the values.While taking the average over all the grades variable
'pazymiai' stores just the last entered variable. In order to complete the job you can use an 'array' to stores sum of grades in each array element .
cin>>pazymiai;
a[k]+=pazymiai;
where as while calculating grades use
vidurkis = a[k] / isvisopazymiu;

C++ while loop not starting [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So the idea is to ask the user for each element of the array, but after an input is given for the first question (where it asks for the amount of elements), nothing happens. Can't figure out why.
#include <iostream>
int main()
{
int numGrades;
tryAgain:
std::cout << "Enter number of grades" << std::endl;
std::cin >> numGrades;
if (numGrades > 30)
{
std::cout << "Please enter a valid number of grades" << std::endl;
goto tryAgain;
}
int grades[numGrades - 1];
int gradeCount = 0;
while (gradeCount < numGrades);
{
std::cout << "Enter grade number" << gradeCount + 1 << ":";
std::cin >> grades[gradeCount];
++ gradeCount;
}
std::cout << grades;
return 0;
}
The constuction while (true); means while (true) {} (i.e. infinite loop).
So, when you write
while (gradeCount < numGrades);
{
// ...
}
you have the following:
while (gradeCount < numGrades)
{
}
{
// ...
}
Second block will never be executed if gradeCount < numGrades.
You are using
while (gradeCount < numGrades);
with a semi-colon (;) at the end of this line so the next line will not exectue because the condition is always true as there is no increment or decrement in the respective variables.
In short just remove the (;)
while (gradeCount < numGrades)
Please see this code, there were few problems. One is semicolon on while loop & another one is printing grades & memory allocation of the grades. Memory static allocation must need a constant value. Here a dynamic allocation is added as the grades number is not fixed or constant... Here is the code:
#include <iostream>
int main()
{
int numGrades;
tryAgain:
std::cout << "Enter number of grades" << std::endl;
std::cin >> numGrades;
if (numGrades > 30)
{
std::cout << "Please enter a valid number of grades" << std::endl;
goto tryAgain;
}
int *grades = (int *)malloc(numGrades * sizeof(int)); //allocating dynamic memory
int gradeCount = 0;
while (gradeCount < numGrades)
{
std::cout << "Enter grade number" << gradeCount + 1 << ":";
std::cin >> grades[gradeCount];
++ gradeCount;
}
for(int i =0;i<numGrades;i++)
{
std::cout << grades[i] << std::endl;
}
free(grades);//releasing memory
return 0;
}