C++ How to fill an array with user input 🌭

preview_player
Показать описание
#C++ #input #array

C++ enter user input into an array tutorial example explained
Рекомендации по теме
Комментарии
Автор

#include<iostream>

int main()
{
std::string foods[5];
int size =
std::string temp;

for(int i = 0; i < size; i++){
std::cout << "Enter in food you like or 'q' to quit #" << i + 1 << ": ";
std::getline(std::cin, temp);
if(temp == "q"){
break;
}
else{
foods[i] = temp;
}
}

std::cout << "You like the following food:\n";

for(int i = 0; !foods[i].empty(); i++){
std::cout << foods[i] << '\n';
}

return 0;
}

BroCodez
Автор

Good Video. Let's defeat the YT algorithm.

FrederikWollert
Автор

So i'm pretty sure using !foods.empty in the for loop like that, will make it out of bounds when entering 5 elements. So I changed this, and i'm sure its better, nested inside the for loop add the !foods thing in a if statement. Like this.
for(int i = 0; i < size; i++){
if(!foods[i].empty()){
std::cout << foods[i] << '\n';
}
}
Thanks so much bro code for your tutorials man, I'm learning so much!

lastwaveundergroundsaviour
Автор

Do we really have to make a new variable "food" to call out the input value? Can't we display the value thru cout and put the variable "foods" to display the input value?

And why exactly we divide the getsize foods[5] to getsize foods [0] where as the second one has no value?

lanvirrusselacosta
Автор

hi, is it normal if i have difficulties understanding some of theses concepts?(this one wasn't hard but i've been having a hard time on these last videos i watched)

rykuthebigryku
Автор

I get a "Segmentation error", but if I use 'q' before finishing the 5 foods the error does not show

virtually
Автор

#include <iostream>

using namespace std;

int main()
{
string foods[5];
string temp;

for (int i = 0; i < size(foods); i++)
{
cout << "Enter food what u like or 'q' to quit #" << i + 1 << ": ";
getline(cin, temp);
if (temp == "q") {
break;
}
else {
foods[i] = temp;
}
}

for (int i = 0; i < size(foods) && !foods[i].empty(); i++)
{
cout << foods[i] << "\n";
}

return 0;
}

WaaABwvoGkiiCNXFQUnlMroDVoMmqy
Автор

for (int i = 0; !foods[i].empty(); i++) - I've never thought about doing this check before, expanding my Thinker

Dazza_Doo
Автор

anyone willing to share their contact info .i am new to coding anyone new wanna help each other out anyone

zdofficial