trying to read input from console & write to file

I'm perplexed.

Assignment I've given myself is to input a filename from the console. Create a file of that name. Then input a number and then output that number to the file. Then close the file.

So far the program does fine for inputting the filename and creating that file. But it won't build from the point "cin.getline(c_number[80]

I'm getting the following messages from the compiler:

C:Documents and SettingsJimMy DocumentsC++ ProgramsNew FolderLearning to create and save to filemain.cpp|29|error: no matching function for call to 'std::basic_istream<char, std::char_traits<char> >::getline(char&)'|

c:program filescodeblocksmingwbin..libgccmingw324.4.1includec++istream|593|note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]|

c:program filescodeblocksmingwbin..libgccmingw324.4.1includec++istream|405|note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]|

The bolded numbers in the above errors are the line numbers that are referenced. The thing about this is that the program I wrote is only 37 lines long.

Here is the program:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>
#include <time.h>

using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main ()
{
    char filename[81];
    char c_number[81];
    int i_number;

    cout << "Enter a filename and press ENTER:  ";
    cin.getline(filename,80);
    ofstream file_out(filename);
    if (! file_out)
    {
        cout << "File " << filename;
        cout << " could not be opened." << endl << endl;
        return (-1);
    }

    cout << "File: " << filename << " was opened." << endl << endl;

    cout << "Enter an integer and press ENTER: " << endl;
    
//  This is where the compiler is generating the error.
    cin.getline(c_number[80]);     
    i_number = atoi(c_number[80]);
    file_out << "This is the number as ascii: " << c_number << endl;
    file_out << "This is the number as an integer: " << i_number << endl;

    file_out.close();

    return (0);
}



Undoubtedly this is a result of lack of experience with the C languages and my 25 years or so since the last time I did any real programming. I would appreciate it if you would instruct me as to what I did wrong and how I can to that which I want to do for this program.

Thank you.

Have a Great Day,
Jim
EGADS ... I fought with this for an inordinate amount of time. Give up and post a question. Then less then 10 minutes after posting the question I see the problem.

Thanks for looking.

Have a Great Day,
Jim
The problem is that you are passing the array with subscript, which will result in a character

cin.getline(filename,80); right
cin.getline(c_number[80]); wrong.
The same applies to the call to atoi.

You may want to look into std::string, they are much better than char arrays
http://www.cplusplus.com/reference/string/
Thank you Bazzy for taking the time to help!!! It is much appreciated.

Have a Great Day,
Jim
Topic archived. No new replies allowed.