Help Please. Add String to existing Array??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

21st Sep 2021, 8:33 PM
Ragnar Mxyzptlk
Ragnar Mxyzptlk - avatar
13 Answers
+ 4
Ok
28th Sep 2021, 6:27 AM
Aarti Gound
Aarti Gound - avatar
+ 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
22nd Sep 2021, 1:32 AM
Ipang
+ 3
Plz guys help me
22nd Sep 2021, 3:30 PM
Aarti Gound
Aarti Gound - avatar
+ 3
Code c++
22nd Sep 2021, 3:31 PM
Aarti Gound
Aarti Gound - avatar
+ 3
Output 3and n=4,k=1(4,3,1,2,5)
22nd Sep 2021, 3:31 PM
Aarti Gound
Aarti Gound - avatar
+ 2
Thank u all for your answer. I think i gathered enough information to evolve. :D
22nd Sep 2021, 7:26 AM
Ragnar Mxyzptlk
Ragnar Mxyzptlk - avatar
+ 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!
22nd Sep 2021, 4:24 PM
mesarthim
mesarthim - avatar
+ 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
21st Sep 2021, 9:27 PM
mesarthim
mesarthim - avatar
+ 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.
21st Sep 2021, 9:47 PM
Ragnar Mxyzptlk
Ragnar Mxyzptlk - avatar
+ 1
Ragnar Mxyzptlk Press 1 then press enter, write your food name and submit. It works :) 1 Newfoodname
21st Sep 2021, 9:49 PM
mesarthim
mesarthim - avatar
+ 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.^^
21st Sep 2021, 10:02 PM
Ragnar Mxyzptlk
Ragnar Mxyzptlk - avatar
+ 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. :)
21st Sep 2021, 10:07 PM
mesarthim
mesarthim - avatar
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 :/
22nd Sep 2021, 1:58 PM
Ragnar Mxyzptlk
Ragnar Mxyzptlk - avatar