-1
#include <stdio.h>
#include <conio.h>

int main()
{
    int Number_of_words, lower_limit = 1;
    char word;

    printf ("Number of Words: ");
    scanf ("%d",&Number_of_words);

    for (lower_limit = 1; lower_limit <= Number_of_words; lower_limit += 1)
    {
        printf("Word: ");
        scanf("%str",&word);

    }
}

so, I want to make a code that will allow the user to input a number of words as many as the number that has been input by the user before. For example,when I run the code, if the input is "4" on "Number of Words", that will allow me to write 5 words. But instead of allowing me to write 4 words, it asks me to input more than 5 words.

(image)It keeps asking more than 4 words

TooGuilty
  • 11
  • 4
  • You should store them in an array and if you don't want to print "Word:" more than once, move it outside the loop – Ramesh Reddy Jan 06 '20 at 09:35
  • please only tag the language you are using. Your code is c++ not c. – 463035818_is_not_an_ai Jan 06 '20 at 09:36
  • `%s` requires a char array big enough to store all the input characters plus a terminating NUL. You have given it one `char` which results in undefined behaviour. – kaylum Jan 06 '20 at 09:37
  • ...however, the only c++ is `using namespace std;` which you could safely remove. Is this supposed to be c++ or c? – 463035818_is_not_an_ai Jan 06 '20 at 09:37
  • Trying to read a C string into one char will not go well. You need an array of char if you wish to read anything bigger than an empty string, ie. one NUL:( – Martin James Jan 06 '20 at 09:37
  • @formerlyknownas_463035818 I'm sorry, Im pretty new with C++. I watched some tutorials on youtube and some of them write *using name space std ;* . – TooGuilty Jan 06 '20 at 09:47
  • @Ramesh How to store them in an array? Im sorry im pretty new with this language. – TooGuilty Jan 06 '20 at 09:47
  • better read [this](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and better learn from a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Most online tutorials are really bad. By the time you realize their mistakes you do not need them anymore, hence they arent really good for beginners – 463035818_is_not_an_ai Jan 06 '20 at 09:49
  • @TooGuilty Instead of us posting an answer a better thing would be if you start learning the basic data structures that'll surely clear up your doubts. – Ramesh Reddy Jan 06 '20 at 09:50
  • And when you learn C++, you should learn proper C++. With streams and standard containers. Your code is C. Also you should not follow tutorials that don't explain _why_ they do things. Like `using namespace std;`, which [is considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). It's not some magic line of code that has to be there. Actually it serves no purpose at all here. – Lukas-T Jan 06 '20 at 09:56
  • I would recommend to change the tag to C. There is no C++ in the shown example. I think this is a C Question- – A M Jan 06 '20 at 10:33
  • Does this answer your question? [Read a string as an input using scanf](https://stackoverflow.com/questions/35103745/read-a-string-as-an-input-using-scanf) – rezaebrh Jan 06 '20 at 12:58
  • @ArminMontigny Will do, thanks. – TooGuilty Jan 06 '20 at 13:27

2 Answers2

1

scanf("%str",&word); causes undefined behavior since word is pointing to character, not a character array. Thus anything may happen.

Side note: "%str" should have been "%s".

Here's how you can do it in C++:

#include <cstddef>
#include <iostream>
#include <string>

int main() {
  std::size_t sz;
  std::cout << "Number: ";
  std::cin >> sz;

  for (std::string s; sz-- && std::cin >> s; /*nothing*/) {
    std::cout << s << '\n';
  }
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • @Ayxan are there any specific differents between doing it in C and doing it in C++? I'm sorry I'm pretty new. I just started studying C for college (1st year) just a few days ago and the previous language that I learned was Python (which was much easier). – TooGuilty Jan 06 '20 at 11:44
1

You are using the wrong format specifier for scanf in your code, c++ has std::cout and std::cin from <iostream> for formatted in/output. Next, a char holds a single character not a string. For strings there is std::string. Moreover, using namespace std; is considered bad practice, it is better to declare variables only when you need them, and it is common convention to reserve capital starting letters for types or constants.

The corrected code could look like this:

#include <string>
#include <iostream>

int main() {
    size_t number_of_words;   
    std::cout << "Number of Words: ";
    std::cin >> number_of_words;

    for (size_t i=0;i<number_of_words;++i) {
        std::string word;
        std::cout << "word: ";
        std::cin >> word;
    }
}

If you want to actually store all the words for later use, you should use a std::vector<std::string> to store them.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you. Are there any specific differents between doing it in C and doing it in C++? – TooGuilty Jan 06 '20 at 11:46
  • @TooGuilty c and c++ really are two different languages. The code you posted looks like c (except the `using namespace std;`) and after fixing the errors it is also valid c++, but in general it is a myth that valid c is also valid c++. The thing is: I dont know C, so I cannot tell you how one would do it in c – 463035818_is_not_an_ai Jan 06 '20 at 12:09