#question

24 messages · Page 1 of 1 (latest)

meager lark
#

Is there a way to have text/instructions in my input.txt file so a user knows what to put accordingly? (new to coding so if this isnt possible then hahahaha)

atomic breachBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

dark isle
#

“Anything is possible”

#

Yes you can read in txt files and print it out like by line, you can design some logic to know when you should be getting user input

#

Maybe you can get input after every line in the txt, or have a special character to indicate to get user input every few lines

#

But if your want user input to branch and do different things, then you’ll need some more refined structure

meager lark
dark isle
#
#include <iostream>
#include <fstream>
#include <string>

int main() {
  std::ifstream infile("data.txt");
  std::string s;
  int i = 1;
  while (std::getline(infile, s)) {
    std::cout << "line " << i++ << " " << s << std::endl;
  }
}
#
data.txt:
awegahwehawe
awegahwehawenaw
enawne awegahwehawewe awehawehaw eh
#

output:

line 1 awegahwehawe
line 2 awegahwehawenaw
line 3 enawne awegahwehawewe awehawehaw eh
meager lark
sly folioBOT
#
Why Is `using namespace std` Considered Bad Practice?

using namespace std will import all of the symbols from std into the enclosing namespace. This can easily lead to name collisions, as the standard library is filled with common names: get, count, map, array, etc.

A key concern with using namespace std; is not what is imported now but rather what may suddenly be imported in the future.

While using namespace std; is alright for tiny projects, it is important to move away from it as soon as possible. Consider less intrusive options, if you insist on not using scope resolution: cpp // OK: *only* import std::vector using std::vector; // OK: namespace alias namespace chr = std::chrono; chr::duration x;

meager lark
#

I ran the code but my input.txt file is still empty?

cyan sapphire
#

the while loop exits when the value returned by getline evaluates to false

#

nvm

#

you aren't actually outputting to the file anywhere, you're only creating the ofstream

dark isle
#

this is very misleading

#

your original question is " text/instructions in my input.txt file so a user knows what to put accordingly"

#

this suggest a prompt so the user knows what to put

meager lark
#

yeahhh

dark isle
#

there is many ways to achieve this, here's one

#
#include <iostream>
#include <fstream>
#include <string>

int main() {
  std::ofstream outfile("data.txt", std::ios::trunc);
  std::streambuf *coutbuf = std::cout.rdbuf();
  std::cout.rdbuf(outfile.rdbuf());
  std::string word;
  while (true) {
    std::cin >> word;
    std::cout << word << "\n";
  }
}
atomic breachBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.