-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUserInput.cpp
More file actions
32 lines (26 loc) · 998 Bytes
/
ArrayUserInput.cpp
File metadata and controls
32 lines (26 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
int main()
{
std::string foods[5]; // array of strings, STATICALLY ALLOCATED CANNOT CHANGE SIZE DURING RUNTIME
int size = sizeof(foods)/sizeof(foods[0]); // calculates number of elements in array
std::string temp;
for(int i = 0; i < size; i++)
{
std::cout << "Enter a food you like or 'q' to quit #" << i+1 << ": ";
std::getline(std::cin, temp); //store input in temp
if(temp == "q") //checks if temp is q, if q then break out of loop
{
break; // breaks out of the loop
}
else // if not q then stores temp value in the index of array, this way we dont get q in the array
{
foods[i] = temp; // assigns the value of temp to the array at index i
}
}
std::cout << "You like the following food:\n";
for(int i = 0; !foods[i].empty(); i++)
{
std::cout << foods[i] << '\n'; // prints the variable stored at the memeory address
}
return 0;
}