Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [C++}  (Read 1277 times)

0 Members and 1 Guest are viewing this topic.

PoeFacedKilla

Prussian Killer Bee
[C++}
« on: March 20, 2011, 08:01:54 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
Ok, so i've written a small program to test my knowledge of file i/o. The source code compiles, and creates an excutable. But when I run the program, after I input the first variable a window pops up (im using vista) and says the program stopped running. Can anyone compile this and see if they get the same error, or can you tell me how to fix it?

Code: [Select]
   #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    char* write_to_file( char* file_src, char* file_txt )
    {
          
          ofstream myFile;
          myFile.open( file_src );
          myFile << file_txt;
          myFile.close();
          
          }
          
    int main()
    {
        
        char* input_src; char* input_txt;
        cout << "Please Enter The File Name: ";
        cin >> input_src;
        cin.get();
        system("CLS");
        
        cout << "Enter Text to be Displayed in File: ";
        cin >> input_txt;
        cin.get();
        system("CLS");
        
        write_to_file( input_src, input_txt );
        cout << input_txt; cout << " - Written to File";
        cin.get();
        
        return 0;
        
        }

any help is greatly appreciated.
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine
Re: [C++}
« Reply #1 on: March 20, 2011, 09:15:07 pm »
  • Wooper Don't Give a !@#$%
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1455
Well first thing you should do in scenarios like this is run the program in Debug mode and find at what line the program crashes

Tell us what line it gets stuck on
Logged


~The Gaurdians of ZFGC~
Kirby, metallica48423, Max, Vash, walnut100
  • Gamers & Developers Unlimited

Xiphirx

wat
Re: [C++}
« Reply #2 on: March 20, 2011, 11:01:16 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3006
Try this

Code: [Select]
#include <iostream>
    #include <fstream>
   
    using namespace std;
   
    void write_to_file( char* file_src, char* file_txt )
    {
         
          ofstream myFile;
          myFile.open( file_src );
          myFile << file_txt;
          myFile.close();
         
          }
         
    int main()
    {
       
        char* input_src; char* input_txt;
        cout << "Please Enter The File Name: ";
        cin >> input_src;
        cin.get();
        system("CLS");
       
        cout << "Enter Text to be Displayed in File: ";
        cin >> input_txt;
        cin.get();
        system("CLS");
       
        write_to_file( input_src, input_txt );
        cout << input_txt; cout << " - Written to File";
        cin.get();
       
        return 0;
       
        }

Logged
  • For The Swarm
Re: [C++}
« Reply #3 on: March 20, 2011, 11:05:36 pm »
  • Doesn't afraid of anything.
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6973
I'm gonna go out on a whim and say it's because your using cin on a pointer, so it's expecting a memory address.  Try this for cin:

cin >> *input_src;

If that still doesn't work for you, then my suggestion is to move away from char pointers and just use an std::string instead.  If you need the data in char form, you can use std::string.c_str().
Logged



i love big weenies and i cannot lie
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.051 seconds with 29 queries.

anything