Whats wrong with my program?

This is a discussion on Whats wrong with my program? within the C++ Programming forums, part of the General Programming Boards category; The program's purpose is for the user to create a new file name along with the file extension. Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    Whats wrong with my program?

    The program's purpose is for the user to create a new file name along with the file extension.

    Code:
    #include <iostream>
    #include <fstream.h>
    #include <string>
    
    void Stream(char &file, char &ext, ofstream &create)
    {
     cout << "Type in a filename: ";
      cin >> file;
     cout << "\nType the file extension(ie: txt): ";
      cin >> ext;
     const char *filename=file + "." + ext;
     create.open(filename);
     create.close();
    }
    
    int main(int argc, char *argv[])
    {
     char boob, lama;
     ofstream x;
     Stream(boob, lama, x);
     cout << "\nFile created was: " << boob << "." << lama << endl;
    
     cin.get();
     return 0;
    }
    Last edited by Golden Bunny; 05-23-2002 at 02:53 PM.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    7
    ermmm...dont ya want - file + "." + ext
    instead of - ext + "." + file

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    yeah, my fault. But besides that. It compiles well but it won't actually create the file.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    7
    I havent used C++ in years, dont you need to specify the entire path when creating and opening a file?

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >But besides that. It compiles well...

    More by luck than judgement, try something like -

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void Stream(string &file, string &ext, ofstream &create)
    {
     cout << "Type in a filename: ";
      cin >> file;
     cout << "\nType the file extension(ie: txt): ";
      cin >> ext;
     string filename=file + "." + ext;
     create.open(filename.c_str());
     create.close();
    }
    
    int main(int argc, char *argv[])
    {
     string boob, lama;
     ofstream x;
     Stream(boob, lama, x);
     cout << "\nFile created was: " << boob << "." << lama << endl;
    
     cin.get();
     return 0;
    }

  6. #6
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    Why not just...
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char filename [15];
        
        cout<<"Enter file name and extension.\n";
        cin.getline (filename, 15);
    
        ofstream file (filename);
        
        file<<"This is a file";
        file.close();
      
        cout<<"File created was "<<filename<<".\n";
        system("pause");
        return 0;
    }
    The only difference here is the user will have to type the "." themselves...but that's not much of a problem is it?
    Last edited by Ion Blade; 05-23-2002 at 03:47 PM.
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    #include //<iostream>
    #include <fstream.h>
    #include <string>
    
    #define arraySize(x) sizeof(x)/sizeof(x[0])
    
    void Stream(char &file, char &ext, ofstream &create)
    {
     cout << "Type in a filename: ";
      cin >> file;
     cout << "\nType the file extension(ie: txt): ";
      cin >> ext;
     char *filename = new char[arraySize(file) + arraySize(ext)];
      strcat(filename, file);
      strcat(filename, '.');
      strcat(filename, ext);
     create.open(filename);
     create.close();
    }
    
    int main(int argc, char *argv[])
    {
     char boob, lama;
     ofstream x;
     Stream(boob, lama, x);
     cout << "\nFile created was: " << boob << "." << lama << endl;
    
     cin.get();
     return 0;
    }
    I'm pretty sure filename can't be constant... as it's created dynamically. Also, you were adding "." to a char, not only can't you use the + operator on chars, it would be '.' and not "."

    Ah well, hope this helps.
    Edit:: I'm pretty sure iostream.h is included in fstream.h, so you don't need to include the iostream header.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    I like Sorensen's version a little better. I hate using chars, but I got a compiling error early that says you cannot use strings in iostream:pen( );
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 02:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 10:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21