0

Basically I'm trying to get a number for cin, but when I use the switch statement, it skips over the first (0) and executes all the subsequent switch statements. I know I purposely left off a file or two at the end.

I'm trying to figure out how to make simple console program that simply reads from simple text files that can be expanded. It paints a menu to the console like a batch file

MENU:

Select 1) Enter name/string to first file
Select 2) Enter name/string to 2nd file
Select 3) Enter name/string to 3rd file
Select 4) Enter name/string to 3rd file

Select X) add file option to menu (aka add another file)

Select X) Combine names in all possible combinations from file1/file2/file3/etc (this can be a command line argument), since the input would be the text file names and it would just read down the list and output a CSV/Text file with all possible combinations of words.

Say: Names1.txt, names2txt, etc.

I've found some code on the web that kinda does something heading in the direction but not sure how to modify it, it just randomly selects from a distribution, but the code appears pretty solid.

Basically I just want to input words into seperate files from the command console from the menu and then run an algorithm that can combine any combination of word from any combination of file.

Say there were two files with 4 words in each, or 5 files with 5 words in each and the algo would just tick down the file and

combine each word from each column them all in a list with a space and output it to a text file.

Red, blue
sally, Mod

New file:

red blue
red mod
sally blue
sally mod
blue red
blue sally
mod red
mod sally

Hope this helps. Basically just making a simple "X" name generator that can be expanded (aka you can use it for anything, and you just have files named XX_item to organize the words input from the console. So you you aren't tied to a specific input.

#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <windows.h>
#include <thread>
#include <stdio.h>

#define BUFFER_SIZE 200



using namespace std;

int main() {

   ofstream myfile ("stud.txt");
   ofstream myfile2("stud2.txt");
   ofstream myfile3("stud3.txt");
   ofstream myfile4("stud4.txt");
   ofstream myfile5("stud5.txt");[tag:tag-name]
   ofstream myfile6("stud6.txt");

   if (myfile.is_open())
   {
   myfile << "This is a line.\n";
   myfile << "This is another line.\n";

   string i;
   cout << "*************************************" << "\n";
   cout << "****** Enter some data bitch  *******" << "\n";
   cout << "*************************************" << "\n";
   cout << "1. Add file" << "\n";
   cout << "2. Add file" << "\n";
   cout << "3. Add file" << "\n";
   cout << "4. Add file" << "\n";
   cout << "5. Add file" << "\n";
   cout << "6. Add file" << "\n";
   string n = "exit";
   char ch;
   cin >> ch;
       
   switch (ch) {
   case '0':
   while (!std::getline(std::cin, i));
   {
   myfile << i;
   cout << "Case 1\n";
   }
   case '1':
   while (!std::getline(std::cin, i));
   {
   myfile << i;
   cout << "Case 1\n";
   }
   case '2':
   while (!std::getline(std::cin, i));
   {
   myfile2 << i;
   cout << "Case 2\n";
   }
   case '3':
    while (!std::getline(std::cin, i));
    {
    myfile3 << i;
    cout << "Case 3\n";
    }
    case '4':
    while (!std::getline(std::cin, i));
    {
    myfile4 << i;
    cout << "Case 4\n";
    }
    case '5':
    while (!std::getline(std::cin, i));
    {
    myfile5 << i;
    cout << "Case 5\n";
    }
    // myfile << i;
    myfile.close();
    }

    }

Basically looping the input and the menu while reading and writing to multiple files is stumping me because of when I use cin to select it skips over the code of the selected item and executes all subsequent items.

I put enough info in the OP I think.

Marek R
  • 32,568
  • 6
  • 55
  • 140
oMNO33
  • 1
  • 5
    Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – BoP Jul 31 '23 at 13:44
  • 2
    `myfile2` anytime you put a number after a variable name probably means you want an array. – drescherjm Jul 31 '23 at 13:50
  • 3
    None of your cases have a `break;` so they just continue on to the next line of code in the next case. – Retired Ninja Jul 31 '23 at 13:56
  • 1
    The loops `while (!std::getline(std::cin, i));` either read exactly one string or fail and get stuck in the loop forever. – molbdnilo Jul 31 '23 at 14:02
  • I am aware how switch works, switch will execute all switch statements, aka in my switch above, the getline continues to getinput from the keyboard for multiple files. That is why there is no break. The idea is to get names in a loop add them to text files. then have a batch menu that spits out combinations drawn from multiple files. – oMNO33 Jul 31 '23 at 14:02
  • 1
    Some strange code and no clear question. I'm having trouble working out what is being asked here. Could you clarify. A focussed question, rather than 'I want so and so', will help. – john Jul 31 '23 at 14:08
  • Program that has a menu that you select from that takes input from the keyboard in a loop and writes and appends it to multiple files, aka a name generator but you generate names by executing the program and adding them to the list, then it writes csv/etc, in a loop, aka the whole program works like a batch file. With menu options. – oMNO33 Jul 31 '23 at 14:10
  • 1
    Why use a switch then? If you want to try to write to multiple files make a `std::vector output_files{ "stud.txt", "stud2.txt", ... };` and loop over that. That is what (range based) for loops are for. (Note : stop using `using namespace std`) – Pepijn Kramer Jul 31 '23 at 14:10
  • @oMNO33 You have a program that has a menu you select from. What exactly is it that you are unhappy about with the current code? – john Jul 31 '23 at 14:11
  • Get input from keyboard (loop) write each input appended to a text file for each INPUT (aka input 1 goes to textfile1, input 2 goes to textfile2) then spit out all possible combinations from each text file that the input was dumped into and dumped into its own text file. – oMNO33 Jul 31 '23 at 14:16
  • @oMNO33 If I'm understanding right, I think your logic is wrong. If you want to read from a file and write to multiple files, you need **one** while loop. the multiple while loops you have currently will never work. It's as if you are trying to execute them all simultaneously, but that's not how C++ works. – john Jul 31 '23 at 15:12

0 Answers0