Free webinar! How to Make Generative AI Work for You
Get my free ticket+ 1
Help Please. Add String to existing Array???
I made a programm to give a Randomfood from an Array of Strings. But now i want to implement that this existing Array is able to save new Foods besides the existing with an input. But i don't know how to start. I searched a lot online but did not find a fitting Solution for my Problem #include <iostream> #include <string> using namespace std; int main() { srand(time(0)); std::string food [4] = {"Pizza","Shiesh", "Burger","Sushi"}; std::string random; for (int i=0;i < 1; i++) { random = food[rand() % 4]; cout << random << endl; } return 0; }
13 Answers
+ 4
Ok
+ 3
You can use std::vector which is a dynamic container that allows addition or removal of items hassle free.
https://www.sololearn.com/learn/261/?ref=app
+ 3
Plz guys help me
+ 3
Code c++
+ 3
Output 3and n=4,k=1(4,3,1,2,5)
+ 2
Thank u all for your answer. I think i gathered enough information to evolve. :D
+ 2
Aarti Gound If you have a problem, please ask with own account in Q&A part. Here, Ragnar Mxyzptlk asked problem and not correct place I think. Thanks for understanding.
Happy coding!
+ 1
I wrote something. I hope, it gives you some clue about what you want to do.
Happy coding!
https://code.sololearn.com/cu1oCcu2A4b4/?ref=app
+ 1
I like your style of coding. Its easy to follow. But still after pressing 1, there is no way of typing in the new food. The programm just stops after. The programm should take 1 or more new Foods, add them to the existing and than choose one randomly.
+ 1
Ragnar Mxyzptlk
Press 1 then press enter, write your food name and submit. It works :)
1
Newfoodname
+ 1
Okay great, that help alot. I will try to implement this in my project. :)
Is it possible to make the newfood into a Array? So i am able to input multiple Foods and than put those 2 arrays together?
This is the last question, i swear.^^
+ 1
Ragnar Mxyzptlk
Yeah, you can do it buddy. Actually, I thought this also. But I didn't for now, cause I wanted to show you my code if it is good or not. :)
0
So i reworked the Code a little bit with a vector.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
srand(time(0));
vector <string> food;
std::string input[] = {};
std::string random;
cin >> input;
food.push_back(input);
for (int i=0;i < 1; i++) {
random = food[rand() % 4];
cout << random << endl;
}
return 0;
}
The Problem is that im not able to fill the array with mutiple strings So the for loop can take one random string. I solved the Problem with one Single string. But thats all :/