-1

So I'm just starting in C++, so I'm not familiar with the language, though I do have knowledge of C. I'm trying to print words vertically. Here is the problem given.

Create an array of 25 strings.

Use a sentinel loop that reads from cin until the array is full or the end of input is reached (when the user presses Ctrl-D), whichever comes first.

After the sentinel loop is over, use a for loop to move through the array.

Remember not to travel farther than the last array element that was input.

Print one array element (one string) followed by a newline

Use a for loop to move through the characters of the string you just printed print one character followed by a newline

#include <iostream>
#include <string> 
#include <iomanip>

using namespace std;

int main()
{

    char word;
    int count = 0;

    cout << "Enter a word: (press Ctrl-D to quit)";

    cin >> word;

    int array1[25];

    while (!cin.eof())
    {

        count = count + 1;
        cout << "Enter a word: (press Ctrl-D to quit)";

        cin >> word;
    } //end while

    for (word = 0; word <= array1[count]; word++)
    {

        cout << 'end1' << 'end1' << "There were " << count << "Words Entered" << 'end1';

    }

} //end main

Code is rough, it compiles, but when it is in an infinite loop with numbers comes out after the texts.

user4581301
  • 29,019
  • 5
  • 26
  • 45
  • The code never puts anything into the array. Also, the instruction is *"Create an array of 25 strings."* not *"create an array of 25 ints"*. So I think you need to crack open that textbook and start reading. – user3386109 Oct 24 '19 at 22:51
  • if you are familiar with C, you should know why this will not work. `int array1 [25];` has 25 elements only. `count can be any number > 0`, so say count is 250, `array1 [250]` - out of bounds. Also as said in the comment above this code does nothing and still causes out_of_bounds – Eugene Anisiutkin Oct 24 '19 at 22:52
  • Future bug to watch out for: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Oct 24 '19 at 22:55
  • 1
    Tagging a question with extra languages increases the number of eyes on your problem, but some of those eyes are not interested in solving C++ problems and will be annoyed. – user4581301 Oct 24 '19 at 22:57
  • Future bug to watch out for: `'end1'` is not an `endl`. It is a multi-character [character literal](https://en.cppreference.com/w/cpp/language/character_literal), and not properly defined in C++. – user4581301 Oct 24 '19 at 23:08

2 Answers2

0

Just for the hell of it

#include <iostream>
#include <string> 
#include <iomanip>
#include <vector>

using namespace std;

int main() {

  string word;
  int count = 0;
  vector<string> arrayOfStrings;

  cout << "Enter a word: (press Ctrl-D to quit)";

  while(cin >> word){
    if(count < 25){
      arrayOfStrings.push_back(word);
      count = count + 1;
      cout << "Enter a word: (press Ctrl-D to quit)";
    } else {
      cout << "25 strings was entered";
      break;
    }
  }//end while

  for ( int j = 0; j < arrayOfStrings.size(); j++ ){
    cout << '\n' << '\n' << $j << "-st string entered " << arrayOfStrings[j] << '\n';
  }
}//end main

This code reads exactly 25 strings, remembers them, and even outputs them later. This is just an educational example, which basically ignores memory managment I strongly suggest not to use this in any actual code. It took me about 5 mins to write this.

0

There are a few errors in this code - perhaps if you are familiar with C, then quickly write a version in C and translate it to a more modern "C++ like" version. Perhaps look into std::string and std::vector to make life even easier.

int array1[25]; needs to store strings, therefore it is of the wrong type.

The while (!cin.eof()) loop needs to also check that it doesn't go over the bounds of the above array (i.e. at most 25 words).

The for (word = 0; word <= array1[count]; word++) loop that needs to loop exactly n times, where n is the number of words inputted, i.e. in the above while loop.

patmanpato
  • 673
  • 9
  • 15