i have a few questions.: 1)if i wanted to write a line of code needed to declare a 1-dimensional array named names that can hold 8 string values would this be correct?: char names[8];
2)how would i write the code needed to assign strings to each element of the array names from question 1) above? how would i use a loop to ask the user for the name & assign it to the element?
3)if i wanted to write the line of code needed to declare a vector named myList that can initially hold 5 integer values would this be correct?
vector<int> myList(5);
4)how would i write the line of code to assign 18 to the 6th element of the myList vector from question 3) above...that is 1 past the initial number of elements set for myList?

Recommended Answers

All 7 Replies

>> 1)if i wanted to write a line of code needed to declare a 1-dimensional array named names that can hold 8 string values would this be correct?: char names[8];

Nope what you declared is an array of chars. chars are just 1 character. For example 'a' is a char, '1' is a char.

>>how would i write the code needed to assign strings to each element of the array names from question 1) above

Access the element and use the assignment operator. The r and l value will depend on what type is the variable.

>>if i wanted to write the line of code needed to declare a vector named myList that can initially hold 5 integer values would this be correct? vector<int> myList(5);

Yes, you can also do this : std::vector<int> myList(5,0); That sets
all the 5 values initially to 0.

>>how would i write the line of code to assign 18 to the 6th element of the myList vector from question 3) above...that is 1 past the initial number of elements set for myList?

Many ways. One way is to use the pus_back method.

std::vector<int> vec(5,0); // 0 0 0 0 0
vec.push_back(18); // 0 0 0 0 0 18

i have a few questions.: 1)if i wanted to write a line of code needed to declare a 1-dimensional array named names that can hold 8 string values would this be correct?: char names[8];

No. You have defined a single character array that hold 8 characters only. You need to expand that idea into and array of arrays. There are a couple ways, you need to do some research.

2)how would i write the code needed to assign strings to each element of the array names from question 1) above? how would i use a loop to ask the user for the name & assign it to the element?

Depends on how you finally implement #1
Since it's exactly 8 strings, a for loop would be the best.

i have a few questions.: 1)if i wanted to write a line of code needed to declare a 1-dimensional array named names that can hold 8 string values would this be correct?: char names[8];

No. That would create an array for 8 chars.

2)how would i write the code needed to assign strings to each element of the array names from question 1) above? how would i use a loop to ask the user for the name & assign it to the element?

- Do you know how to loop?
- Do you know how to ask for user input?
- Do you know arrays (or vectors)?

3)if i wanted to write the line of code needed to declare a vector named myList that can initially hold 5 integer values would this be correct?
vector<int> myList(5);

Yes.

4)how would i write the line of code to assign 18 to the 6th element of the myList vector from question 3) above...that is 1 past the initial number of elements set for myList?

Look at the push_back() method

[edit]
Holy cow, a crosspost with Firstperson and WaltP at the same time! At least we all have the same answers :)

i have a few questions.: 1)if i wanted to write a line of code needed to declare a 1-dimensional array named names that can hold 8 string values would this be correct?: char names[8];

char names[8] would not do what you want. That would create an 8-element array with each element holding a single char. You might consider creating a vector of empty strings. vector<string> names(8,""); Another option would be string names[8]

2)how would i write the code needed to assign strings to each element of the array names from question 1) above? how would i use a loop to ask the user for the name & assign it to the element?

You can't. If you were to use vectors, you would write your loop just like any other loop. Use an index value then iterate over the vector using the index value setting it to what you wish. When you're doing the input part build a similar loop but be sure to include your input statements with in the loop in addition to the element-specific assignment statements (can be combined if done correctly).

3)if i wanted to write the line of code needed to declare a vector named myList that can initially hold 5 integer values would this be correct?
vector<int> myList(5);

Yes.

4)how would i write the line of code to assign 18 to the 6th element of the myList vector from question 3) above...that is 1 past the initial number of elements set for myList?

You could either use vector::push_back() or use vector::resize() then assign to element 5. You can find information on both methods here.

[edit]wow...i'm slow....but at least we're consistent[/edit]

>>[edit]wow...i'm slow....[/edit]

Yep. We are super ninjas.

commented: We are indeed :) +12

- Do you know how to loop?
- Do you know how to ask for user input?
- Do you know arrays (or vectors)?

well, i've forgotten how to do certain things in c++. i just need to read over those topics again. thx a lot 4 ur help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.