1 Replies - 847 Views - Last Post: 27 April 2012 - 11:36 PM Rate Topic: -----

#1 captjackAV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-April 12

C++ Creating a List- Printing character array

Posted 27 April 2012 - 10:49 PM

Hi,

I am creating a text editor. My List class is a child of the Editor class. I am asking the user to enter the number of entries they want for the list and let them input a string. My problem is printing the strings in a list format. I cannot accomplish printing the string in the array. here is the code. thank you in advance!

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
#include "List.h"
#include <fstream>
using namespace std;

List::List(string myfile) : Editor(myfile)
{
	myfile;
}

List::~List()
{
	cout << " " << endl;
}


void List::createfile()
{
	string filename;
	ofstream outfile; // object for writing to a file
	cout << "Enter the name of the file you wish to create: " << endl;
	cin >> filename;
	outfile.open (filename.c_str() );
}

[b]void List::createlist()
{
	int i;
	int entry;
	char array[200];

	cout << "Enter the number of entries you would like for your list" << endl;
	cin >> entry;
	for(i=0;i<entry; i++)
	{
		cout << "Enter your item" << endl;
		cin >> array[i];
	}

	cout << "Below displays your list" <<endl;
	for(i=0;i<entry; i++)
	{
		//cin.getline (array[i],200);
		cout << array[i]<< endl;
	}
}[/b]



MOD EDIT: added code tags. Please use them in the future. Thanks!

This post has been edited by sarmanu: 27 April 2012 - 11:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Creating a List- Printing character array

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C++ Creating a List- Printing character array

Posted 27 April 2012 - 11:36 PM

Sounds to me that you are trying to input strings, and not chars? In your case array[i] is just a char, not a char array. If you want to input string, just make "array" a 2D array:
// array[someindex] can contain a maximum of 50 characters,
// not including NULL too
char array[200][51];


And that's it, array[i] is now a char array itself.
But if you are using C++, why not use a STL string, it's way more easier to deal with, more elegant.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1