I am a beginner at c and programming. This is the part of a program that I was working on today that I was struggling with, I don't know if I was doing it right, some of my classmates said that we can use array but I don't know how to use array yet, so I came up with this code, however when it comes to printing the names that I inputted in the variable userInput it only prints the last name that I inputted and not including the first one that I inputted.
If my programming was incorrect please explain what should I do in a way that beginners could understand. Thanks
int main()
{
int data = 0;
int input;
string userInput;
//this program will run repeatedly when user didn't enter a number from 1 to 10 only
while (data == 0)
{
cout << "Enter number of data: ";
cin >> data;
if (!(data > 0 || data < 11))
{
data = 0;
}
else
{
//this program will repeat depends on the input from the variable of data
for (input = 0; input < data; input )
{
cout << "Enter student name: ";
cin >> userInput;
}
}
}
cout << "Student names" << endl;
//this for loop shoul print all names that was inputted in the variable userInput
for (in = 0; insert < data; insert )
{
cout << userInput << endl;
}
return data;
}
OUTPUT
Enter number of data: 0 //the program will repeat when the user didn't input number from 1 to 10 only
Enter number of data: 3 //the program will ask for names according to the input in this line
Enter students name: Jack
Enter students name: King
Enter students name: Queen
Student names
Queen
Queen
Queen
//The output should look like this
Student names
Jack
King
Queen
CodePudding user response:
Each write to userInput variable overrides the previous data stored in the variable. So by the time you get to the for loop to cout userInput, userInput is equal to Queen. A string can only contain a single char array at a time. To fix this you can create a std::vector like WhozCraig recommended.
#include <vector>
int main()
{
int data = 0;
int input;
string userInput;
vector<string> vec; //This will hold the string characters
//this program will repeat depends on the input from the variable of data
for (input = 0; input < data; input )
{
cout << "Enter student name: ";
cin >> userInput;
vec.pushback(userInput);
}
You then can use a range based for loop to print all the values:
//this for loop shoul print all names that was inputted in the variable userInput
for (string insert : vec)
{
cout << insert << endl;
}
CodePudding user response:
As already mentioned you are just overwriting userInput with each new input read. You can simply append the new string to your existing one:
std::string students;
for (int i=0; i<data; it) {
std::string input;
std::cout << "Enter student name: ";
std::cin >> input;
if (!students.empty())
students = "\n";
students = input;
}
But I also highly recommend to use std::vector<std::string> in this case since it would be a mess to extract the student names afterwards from such a concatenated string and you can't use any of the helpful functions these containers provide.
std::vector<std::string> students;
for (int i=0; i<data; it) {
std::string input;
std::cout << "Enter student name: ";
std::cin >> input;
students.push_back(input);
}
// one example of the advantages
std::cout << "Number of students: " << students.size() << std::endl;
And printing when using vector:
for (auto it = students.begin(); it != students.end(); it) {
std::cout << *it << std::endl;
}
