Doubting My Should I Add a Loop?

  • Context: Comp Sci 
  • Thread starter Thread starter asz304
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary
SUMMARY

The discussion centers on the implementation of a loop in C++ for inputting student data, specifically names, letter grades, and marks. The user is advised to utilize a for loop to iterate through the number of students, ensuring that data is collected correctly without redundant array declarations. Key issues identified include the need for a constant value for the array size and the importance of managing array declarations to avoid conflicts. The recommended approach simplifies data entry and enhances code clarity.

PREREQUISITES
  • Basic understanding of C++ syntax and structure
  • Familiarity with arrays in C++
  • Knowledge of loops, specifically for loops in C++
  • Experience with standard input/output operations in C++
NEXT STEPS
  • Implement a for loop for array input in C++
  • Explore dynamic memory allocation for arrays in C++
  • Learn about scope and lifetime of variables in C++
  • Investigate best practices for managing user input in C++ applications
USEFUL FOR

C++ programmers, computer science students, and educators looking to improve their understanding of loops and array management in C++ applications.

asz304
Messages
107
Reaction score
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
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.
 
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
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K