I am trying to make this program work but only getting halfway, i am supposed to have an output like this:

Enter a string at least 8 chrs long: short
The string is too short.
Enter a string at least 8 chrs long: 1234567
The string is too short.
Enter a string at least 8 chrs long: something
The string has 9 chrs in it.
First three: som
Last three: ing
gnihtemos

but the code im working on looks like this, and i dont know how to get the last three char. can some one help!

short
Enter a string at least 8 chrs long: 1234567
Enter a string at least 8 chrs long: something
Enter a string at least 8 chrs long:
The string has 9 chrs in it.
First three: som
Last three:

here is what i got
[
count=0;

while (cin>>word)
{cout << "Enter a string at least 8 chrs long: ";
len = word.length();
count++;
word1 = word.substr(0,3);

}

cout << "The string has " << len << " chrs in it." << endl;
cout << "First three: " << word1 << endl;
cout << "Last three: " << word2 << endl;


]

Problem 1 - your are taking the input before prompting the user. Restructure your loop as:

cout << "Enter a string at least 8 chrs long: ";
while (cin>>word)
{
    len = word.length();
    count++;
    word1 = word.substr(0,3);
    //rest of processing here

    cout << "Enter a string at least 8 chrs long: ";
}

For the last-three problem, you have the parts you need. Using the substr( ) method as you already have, and knowing how long the string is, find the start of the last 3.

As to reversing the string, there are many possibilities. Are you using a compiler that has the strrev( ) function for C-style strings? You could work with that. Or, create a loop that builds a new string, starting with the last character of your input.

Val

i went ahead a wrote it like this and put my cout were you said but i get an extra promt .

[count=0;

cout << "Enter a string at least 8 chrs long: ";
while (cin>>word)
{


len = word.length();
count++;
{if (len<8)
cout << "The string is too short" << endl;
}
firstthree = word.substr(0,3);

cout << "Enter a string at least 8 chrs long: ";

}
{lastthree = word.substr(6,3);}


cout << "The string has " << len << " chrs in it." << endl;
cout << "First three: " << firstthree << endl;
cout << "Last three: " << lastthree << endl;


]

this is what i get


Enter a string at least 8 chrs long: short
The string is too short
Enter a string at least 8 chrs long: 1234567
The string is too short
Enter a string at least 8 chrs long: something
Enter a string at least 8 chrs long:
The string has 9 chrs in it.
First three: som
Last three: ing

First, it's easier to see what's going on when the code is formatted better. Here's your previous code:

count=0;

   cout << "Enter a string at least 8 chrs long: ";
   while (cin>>word)
   {
      len = word.length();
      count++;
      {
         if (len<8)
              cout << "The string is too short" << endl;
      }
      firstthree = word.substr(0,3);

      cout << "Enter a string at least 8 chrs long: ";
   }
   {
      lastthree = word.substr(6,3);
   }

   cout << "The string has " << len << " chrs in it." << endl;
   cout << "First three: " << firstthree << endl;
   cout << "Last three: " << lastthree << endl;

The curly braces at lines 8&11, 16&18 do nothing other than add confusion.

If it is your intent to keep prompting for words until the first occurence of one 8 characters or longer, that needs to be part of the loop condition. Prompting again should then be moved into the if clause.

Here's an ordering that should get you there:

count=0;

   cout << "Enter a string at least 8 chrs long: ";
   cin>>word;
   while ( word.length() < 8 )
   {
      count++;
   
      if (len<8)
      {
         cout << "The string is too short" << endl;
         cout << "Enter a string at least 8 chrs long: ";
         cin>>word;
      }
   }

   len = word.length();
   firstthree = word.substr(0,3);
   //get the last three
   //reverse the string

   //lastthree = word.substr(6,3); //replace the 6 with a variable
                                   //or calculation!

   cout << "The string has " << len << " chrs in it." << endl;
   cout << "First three: " << firstthree << endl;
   cout << "Last three: " << lastthree << endl;

Also, when using the code tags in this forum, you place the full tag [ c o d e ] before your code, and [ / c o d e ] after your code (omitting the spaces). It's not the case of substituting your code for the word between the square brackets, as you've been doing.

Val

i ended up working on the program and finnaly got it and figured out hot to get the last three char, this what i ended up with whiched worked.

len = 1;
while (len < 8)
{
cout << "Enter a string at least 8 chrs long: ";
len++;
cin >> word;
len = word.length();
{
if (len<8)
cout << "The string is too short" << endl;
}
}
firstthree = word.substr(0,3);
lastthree = word.substr(word.length()-3,word.length()-1);

i still cant get my code to print out the input in reverse or backwords. i am trying to print (something) backwards but im stuck, i used word [word.length()-1]; to print but i only get the last char and when i looped it i got a countinous loop. here is the looped i used, i cant seem to get it to stop.

while(word[word.length()])
w = word[word.length()-1];

Your loop condition will always be true, for any non-empty string. word.length( ) will not change to a 0 value.

word.length()-1 tells you where to find the last character, right?
The first character will be at word[0] (or word.at(0)), right?

Can you make a loop, knowing those values?

Im really not confused on how to read the last letter it just i dont no how to use it in aloop, ive been trying for about 4hrs so im taking a break. can you be a little bit more specific on were to put what without giving it away. this is what i got, it prints out (omethin)

while (word[0]=word[word.length()-1])

word[word.length()-1]--;
wordreversed=word;

Consider your word and the index of each character:
s o m e t h i n g
0 1 2 3 4 5 6 7 8

word.length( ) gives you a value of 9

So you need a loop that will iterate from position 8 to position 0. It's really a simple for loop.

~~~~~
The code you just posted is not really doing anything useful
while (word[0]=word[word.length()-1]) //assigns the last char to the first char, again and again,
//and this will always be TRRU

word[word.length()-1]--; //decrements the value of the char at last position g -> f -> e.....
wordreversed=word; //copies current version of word to wordreversed, in same order

thanks vmanes for helping me out, i used the for statment and after trial and error i got it. one again thanks for helping me!!

This article has been dead for over six months. Start a new discussion instead.