-8

This is what I have tried so far. I am pretty sure freopen is getting caught up in the loop somehow. Thank you in advanced for your help! Sorry If I messed up the formatting. Once cin is used once, it doesn't allow for further input and just prints an identical if statement 5 times.

#include <iostream>
using namespace std;
#include <string>
int main(){
    string x;
    string z;
    int y;
    string t;
    for(int i=0; i<6; i++){
        cout<<"name"<<endl;
        cin>>x;
        cout<<"Pokemon name capatalized"<<endl;
        cin>>z;
        cout<<"Pokedex number"<<endl;
        cin>>y;
        cout<<"type"<<endl;
        cin>>t;
       freopen( "file.txt", "a", stdout );
        cout<<"else if(pokename==\""<<x<<"\" || pokename==\""<<z<<"\" || p=="<<y<<")""{cout<<\"This is "<<z<<", \"; "<<t<<"();}"<<endl; 
    }
    return 0;
}
Jack Gruber
  • 163
  • 1
  • 14
  • 1
    What did you expect? What are you observing? What have you tried to debug it? What have you researched? And yeah, you messed up the formatting (fixed it for you). – Jesper Juhl Aug 21 '16 at 18:03
  • 1
    The right tool to solve such problems is to use your debugger, but not to ask at Stack Overflow before you did so. Tell us all your observations you made when inspecting your code stepping through line by line in 1st place. Also you might want to read **[How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** At least leave us with a [MCVE] that reproduces your problem. (This is a personal stock comment provided by πάντα ῥεῖ™) – πάντα ῥεῖ Aug 21 '16 at 18:06
  • 2
    @Jack You don't script in C++. – LogicStuff Aug 21 '16 at 18:28
  • 1
    read a `c++` book for beginners, its good for you. – BattleTested_закалённый в бою Aug 21 '16 at 18:50
  • Also, **always** verify your input was successful **after** tryung to read: the description of your problem implies one of the inputs failed. – Dietmar Kühl Aug 21 '16 at 19:04

1 Answers1

1

If you wish to write to a file you can do so by using the ofstream class: Here is a code snippet that you can use to write to a file:

#include<fstream>
#include<string>

using namespace std;
int main() {
    string file = "myfile.txt";
    ofstream out(file.c_str());

    string output = "writting...";
    out << output;
    out.close();
}

Let me explain what this piece of code does:

The ofstream out statement declares an object of type ofstream. The constructor takes a c_style string as an argument and therefore the method c_str() is used to convert the string class to a c_style string. Then, using the << operator and the ofstream object out I am writing to the file.

KostasRim
  • 2,053
  • 1
  • 16
  • 32
  • Thank you so much! I really appreciate your help. I will try this. Sorry about the shitty question writing, I'm knew to stack. I'm I banned for questions forever now? – Jack Gruber Aug 21 '16 at 18:10
  • 1
    @JackGruber No you are not banned, but I would suggest you to read a good book about c++(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Just read the rules of `stackoverflow` and you will be fine. If this answer solves your problem you can mark it as the solution. – KostasRim Aug 21 '16 at 18:12