Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,964 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,499 people online right now. Registration is fast and FREE... Join Now!




string arrays

 
Reply to this topicStart new topic

string arrays

tonyho25
11 May, 2008 - 02:03 PM
Post #1

New D.I.C Head
*

Joined: 11 May, 2008
Posts: 2

I have an assignment due today and it involves arrays.
The assignment states to " add code to read 5 words from user input (cin). Store these values into an array of string[]. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant "end_of_array" into the last element of the array. Using a do while () loop, print the 1st and 3rd letter (character) of each word (two letters per line) using the substring function.

I have my variable set to : char name[5];

my code is :
cout << "Enter five words with a minimum of six characters. \n Hit the return key when finished. " ;
cin >> name;
cout << name << endl;

It allows the user to enter five words using the space bar in between words and it p[rints the first word. Can someone help me declare a constant variable "end_of_array" and how to do the do...while() loop for the 1 and 3 characters. Please explain how you did it as well so I can actually understand. I am also not sure if my variable is correct because it is suppose to be a string[].
User is offlineProfile CardPM
+Quote Post

tonyho25
RE: String Arrays
11 May, 2008 - 02:50 PM
Post #2

New D.I.C Head
*

Joined: 11 May, 2008
Posts: 2

QUOTE(tonyho25 @ 11 May, 2008 - 03:03 PM) *

I have an assignment due today and it involves arrays.
The assignment states to " add code to read 5 words from user input (cin). Store these values into an array of string[]. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant "end_of_array" into the last element of the array. Using a do while () loop, print the 1st and 3rd letter (character) of each word (two letters per line) using the substring function.

I have my variable set to : char name[5];

my code is :
cout << "Enter five words with a minimum of six characters. \n Hit the return key when finished. " ;
cin >> name;
cout << name << endl;

It allows the user to enter five words using the space bar in between words and it p[rints the first word.

const string = end_of_array;
if that the correct way to do a const for end_of_array.

how to do the do...while() loop for the 1 and 3 characters. Please explain how you did it as well so I can actually understand. I am also not sure if my variable is correct because it is suppose to be a string[].



User is offlineProfile CardPM
+Quote Post

gabehabe
RE: String Arrays
11 May, 2008 - 02:59 PM
Post #3

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,514



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
You have an array of chars, you need an array of strings

We don't usually give out free code, but here:
cpp
#include <iostream>

using namespace std;

int main ()
{
string words[5];
cout << "Please enter 5 words:" << endl;
for (int i = 0; i < 5; i++) // this loop will execute 5 times for input
cin >> words[i];

// now output the words, one line each:
for (int i = 0; i < 5; i++)
cout << words[i];

system("pause");
return EXIT_SUCCESS;
}

Hope this helps smile.gif

This post has been edited by gabehabe: 11 May, 2008 - 03:02 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month