Doubting My Should I Add a Loop?

  • Comp Sci
  • Thread starter asz304
  • Start date
  • Tags
    Loop
In summary, the conversation is discussing the proper way to input student information, specifically names, letter grades, and marks. The suggestion is to use a for loop to prompt for each piece of information instead of manually inputting each one. It is also mentioned that the three arrays in the code need to be declared only once and that the number of students should be a constant known at compile time.
  • #1
asz304
108
0
I'm not sure how this works. If I need to put a loop before the first cout statement.

The Attempt at a Solution



Code:
include <iostream>
include <cmath>
using namespace std;

string name[number];
char letterGrade[number];
double mark[number];

int main(){

string name[number];
char letterGrade[number];
double mark[number];

  cout <<"Enter number of students";
  cin >> number;

  cout << " Enter the name ";
  cin >> name[number];

  cout << " Enter the mark ";
  cin >>  mark[number];

Is this right?
or should there be a loop before the first cout statement that holds till the third cin statement?
 
Physics news on Phys.org
  • #2
if you just want to two numbers then you don't need anyones name first of all. Second, do you plan to input all of these numbers? If so, I would store the numbers first into an array, and then manipulate the arrays separately. running a loop the size of your class and inputting them would be an annoying way to carry through this.
 
  • #3
1. Your three arrays all have number elements, so unless you're doing something fancy with dynamically-allocated arrays, number needs to be a constant that is known at compile time.

2. You have declared the three arrays twice each: once at the gobal level, and once inside main.

3. If you have a small number of student names and their letter grades and marks to enter, you can use a for loop that looks like this:
Code:
for (int i = 0; i < number; i++)
{
  cout << "Student name: ";
  cin >> name[i];
  // Prompt for letter grade in a similar way
  // Prompt for mark in a similar way
}
 

Related to Doubting My Should I Add a Loop?

1. What is a loop in scientific research?

A loop in scientific research refers to a repetitive process of collecting and analyzing data in order to test a hypothesis or answer a research question. It involves multiple iterations of experiments or data collection, with each iteration building upon the previous one.

2. Why is it important to add a loop in my research?

Adding a loop in your research allows you to gather more data and strengthen your findings. It also helps to minimize bias and increase the validity and reliability of your results.

3. How do I determine the appropriate number of loops for my research?

The number of loops needed for your research will depend on the complexity of your question and the amount of data you need to collect. It is important to consider factors such as time, resources, and feasibility when determining the appropriate number of loops.

4. Can a loop be added at any stage of my research?

Yes, a loop can be added at any stage of your research. However, it is best to plan for a loop in the initial stages of your research design to avoid any delays or complications in the future.

5. What are some potential challenges of adding a loop in my research?

Some potential challenges of adding a loop in your research include the need for additional time and resources, potential changes in research protocols, and the possibility of encountering unexpected results. It is important to carefully plan and anticipate these challenges in order to effectively incorporate a loop into your research.

Similar threads

Replies
2
Views
2K
Replies
3
Views
894
Replies
7
Views
1K
Replies
3
Views
1K
Replies
6
Views
3K
Replies
10
Views
2K
Back
Top