0
#include <iostream>


void main()
{
    using namespace std;
    char sentence[2000];
    for (int i = 0; i <= 2000; i++) {
        char Test;
        cin >> Test;

        if (Test != '\r') sentence[i] == Test;
        else break;
    }
        
    for (int i = 0; i <= 2000; i++) {
        cout << sentence[i];
    }
}

Why does my function for inputting a string not work? I wrote this program that is supposed take in input from the user and display the sentence back when the user hits enter. I know there are functions in C++ to do this but I want to write such a program myself. I am a beginner so please keep the answer beginner friendly. I want to know why this doesn't work and also how to make it work. What happens when I run this program is that the input keeps going even thought I hit enter.

EDIT: 2 Alright so after reading the comments. I edited my code to this:

#include <iostream>


int main()
{
    using namespace std;
    char sentence[2000];
    for (int i = 0; i < 2000; i++) {
        char ch;
        cin >> ch;

        if (ch != '\n') sentence[i] = ch;
        else break;
    }
        
    for (int i = 0; i <= 2000; i++) {
        cout << sentence[i];
    }
    return 0;
}

What is expected: The sentence just entered gets outputted back when hitting the enter key.

What happens: The cursor goes to the next line asking for more input.

EDIT: 3 I changed the 2000 to 5 and noticed the behavior. The problem is that the char declaration for ch doesn't detect anything like whitespaces or newlines and therefore the program will only output once 'i' has cycled through fully and we exit the for loop. However I do not know how to fix this. How do I detect the white spaces and newlines?

Caspex
  • 3
  • 2
  • Did you enter the whole sentence: `"like this"`? – justANewb stands with Ukraine Jan 09 '22 at 06:30
  • 1
    Being a beginner, you'll want to get to know your compiler warnings. The `sentence[i] == Test` issue is something that compilers will warn about. https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings. `void main` is a common beginner mistake that should not be accepted by the compiler because C++ requires `int`. Changing your compiler flags can fix that as well. – chris Jan 09 '22 at 06:37
  • @justANewbie Yes – Caspex Jan 09 '22 at 06:44
  • @Caspex Try `'\n'` instead of `'\r'`. Why did you code `'\r'`? – chux - Reinstate Monica Jan 09 '22 at 06:47
  • @chux-ReinstateMonica Doesn't work, same thing. I thought '\r' was for detecting enter. – Caspex Jan 09 '22 at 06:49
  • @Caspex Why output 2000 (+ 1) characters. How about printing `i` characters? Rather than "and doesn't output the string I entered.", report what you entered and what was seen, what was expected. – chux - Reinstate Monica Jan 09 '22 at 06:52
  • Try to find the newline character for your environment. For example, the new line character for unix based system is '\n'. – Imranur Rahman Jan 09 '22 at 06:55
  • @ImranurRahman It's the same for me. I am on windows 10 using visual studio for this. – Caspex Jan 09 '22 at 07:11
  • @chux-ReinstateMonica I do not know how to print i characters. I tried assigning it to new variable but the variable's scope doesn't reach outside the for loop. Also I edited the post to include What was expected and seen. – Caspex Jan 09 '22 at 07:13
  • Please look at Edit 3. – Caspex Jan 09 '22 at 07:29

2 Answers2

1

The problem is with this line:

    if (Test != '\r') sentence[i] == Test;

Here you are using the equality operator ==, when you intend to use the assignment operator =.

Additionally, note that in your for loops, you should use < rather than <= to avoid over-running the length of your array.

sudo make install
  • 5,629
  • 3
  • 36
  • 48
  • I did this, however when I run the program, still when I press enter, instead of the output, it just goes to the next line for more input. – Caspex Jan 09 '22 at 06:37
1

You can change it (here). Try this:

#include<iostream>
using namespace std;

int main(int argc, char * argv[])
{
    char sentence[2000];
    char ch;
    for (int i = 0; i < 2000; i++) {
        cin >> noskipws >> ch;

        if (ch != '\n') sentence[i] = ch;
        else {
            sentence[i] = '\0';
            break;
        }
    }

    for (int i = 0; sentence[i]; i++) {
        cout << sentence[i];
    }
    return 0;
}

The noskipws method of stream manipulators in C++ is used to clear the showbase format flag for the specified str stream. This flag reads the whitespaces in the input stream before the first non-whitespace character.