473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

cstring arrays

Hi there just wondering if I am on the right track. I need to load a file in a program and then store the stata in an array. I am now testing this function to see if it is working... when I run the functioni the following happens.

1) the first number in the txt file that I open is missing. And when I set the list to < 82 (as there is 81 numbers in total) it will show the data and then say there was an error. in fact once it hits over 30 it will come up with this problem. How do I fix that??

2) also how do I get about including the first number in the file being included.

Am I on the right track?

Expand|Select|Wrap|Line Numbers
  1. int loadFile()
  2. {
  3.  ifstream infile;
  4.    string filename;
  5.    float list = 0;
  6.    cout << "Enter file name " << endl;
  7.    cin >> filename;
  8.    infile.open(filename.c_str());
  9.    if (infile.fail())
  10.    {
  11.       cout << "File not found " << endl;
  12.       return EXIT_FAILURE;
  13.    }
  14.    string s;
  15.    infile >> filename[list];
  16.    while (infile && list >= 0 && list <9)
  17.     {
  18.        infile >> filename[list];
  19.        cout << filename[list];     
  20.        list++;    
  21.     }
  22.  
  23.    return EXIT_SUCCESS;
  24. }
  25.  
Feb 27 '09 #1
9 2197
horace1
1,510 Expert 1GB
you read the first character but don't print it. Remove the read before the while loop, e.g.
Expand|Select|Wrap|Line Numbers
  1.    //infile >> filename[list];
  2.    while (infile && list >= 0 && list <9)
  3.     {
  4.        infile >> filename[list];
  5.        cout << filename[list];
  6.        list++;
  7.     }
  8.  
I am not sure what you are tring to do. at present you are reading the data as single characters into a string - if the data is numeric you could read it into an array of ints or floats
Feb 27 '09 #2
wow thanks I didnt realise that that first part took the first character out. Thank you so much. it has been bugging me all day!!

What I am trying to do is prompt my function and read in a file name and then open the file and read in the grid. I then need to store the array (pass by reference) for a second function so that I can actually display the grid... but at the moment I just want to see if I am getting this first part right.... am i making sense??

So yes it is a string of characters. I need to read the data and output it 9 per line (81 characters in total can be assumed) At the moment I have it set to <=9 because it seems to work this way however I need it to read all characters. if I type <= 80 the program comes up with an error and closes down.

does that make sense??
Feb 27 '09 #3
maybe this will make it clearer? I have barely started the program.. I just want to try and work out how to do the first part of it.

using namespace std;

void printMenu();
void loadFile();
void displayFile();
void test();
int main()
{


char choice = '*';
while (choice != 'Q')
{
printMenu();
cin >> choice;
switch (toupper(choice))
{
case 'L' : loadFile();
break;
case 'D' : displayFile();
break;
case 'R' : // complete this
case 'C' : // complete this
case 'M' : // complete this
case 'Q' : break;
default : cout << "Invalid option " << endl; cin.ignore(100,'\n');
}

}

return 0;
}


void printMenu()
{
cout << "\n\tSudoku Checker " << endl << endl;
cout << "\t L\t Load file " << endl;
cout << "\t D\t Display " << endl;
cout << "\t C \t Check columns " << endl;
cout << "\t R \t Check rows " << endl;
cout << "\t M \t Check minigrids" << endl;
cout << "\t Q\t Quit " << endl;
cout << " Rows and columns are labelled from 0 to 8 " << endl;
cout << endl;
}

void loadFile()
{
ifstream infile;
string filename;
float list = 0;
cout << "Enter file name " << endl;
cin >> filename;
infile.open(filename.c_str());
if (infile.fail())
{
cout << "File not found " << endl;
return EXIT_FAILURE;
}
string s;
//infile >> filename[list];
while (infile && list >= 0 && list <80)
{
infile >> filename[list];
cout << filename[list];
list++;
}
//note need help around here as will cout the whole list.. except for the first number and if I make the while terms over 20 it will corrupt.....?
// cout << filename[list];
//system("pause");
return EXIT_SUCCESS;
}
Feb 27 '09 #4
horace1
1,510 Expert 1GB
as I understand it you want to read in a 9 * 9 array of characters
you could have a char array 9*9 and read the characters into it, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int loadFile(char data[9][9])
  8. {
  9.  ifstream infile;
  10.    string filename;
  11.    float list = 0;
  12.    cout << "Enter file name " << endl;
  13.    cin >> filename;
  14.    infile.open(filename.c_str());
  15.    if (infile.fail())
  16.    {
  17.       cout << "File not found " << endl;
  18.       return EXIT_FAILURE;
  19.    }
  20.    for(int i=0;i<9;i++)
  21.      for(int j=0;j<9;j++)
  22.        infile >> data[i][j];
  23.    return EXIT_SUCCESS;
  24. }
  25.  
  26. int displayFile(char data[9][9])
  27. {
  28.     for(int i=0;i<9;i++)
  29.      {
  30.      for(int j=0;j<9;j++)
  31.        cout << data[i][j];
  32.      cout << endl;
  33.      }
  34. }
  35.  
  36. int main()
  37. {
  38.  char data[9][9];
  39.  loadFile(data);
  40.  displayFile(data);
  41. }
  42.  
reads the following file and prints it
Expand|Select|Wrap|Line Numbers
  1. 123456789
  2. 123456789
  3. 123456789
  4. 123456789
  5. 123456789
  6. 123456789
  7. 123456789
  8. 123456789
  9. 123456789
  10.  
could you give us a sample of your data?
Feb 27 '09 #5
you are brilliant, thanks so much. that makes sense.. why didnt I think of adding a float instead of int ?

I need to input a space inbetween the output numbers in the grid

eg.
1 2 3 4 5 6 7 8 9
1 2 3 . . . . . ..

why does it not like it when I do the following? (note: I added a space between array i and j.

for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
cout << data[i] << " " << [j];
cout << endl;
}
Feb 27 '09 #6
dont worry I worked it out.. woohoo thanks so much!!! :)
Feb 27 '09 #7
do you still want a sample of my data.. this is only part of what I have to do.. but I want to try to work out the whole thing first :) I know it wont be easy but you have helped me out a LOT
Feb 27 '09 #8
horace1
1,510 Expert 1GB
I assume you used skipws from <iomanip> to skip the spaces in your file, e.g.
Expand|Select|Wrap|Line Numbers
  1.    for(int i=0;i<9;i++)
  2.      for(int j=0;j<9;j++)
  3.        infile >> skipws >> data[i][j];
  4.  
Feb 27 '09 #9
no i didnt actually.. I have never heard of that before probably because I am fairly new to programming... I did the following:
infile >> data[i][j] >> " ";
Feb 27 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
8
by: Oskar | last post by:
Hi. I`m new in cpp and i have a litlle problem. i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv 234") and i want to put the data (space separated) into an array.It shuld...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
1
by: dave | last post by:
Hi, I would apprecaite the followinghelp; I need to pass a TCHAR * s = foo*.* \0 *h\0" to CString operator, but it will stop reading input at teh first \0, how do I work around this ?? ...
3
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
4
by: huguogang | last post by:
Just curious, any one know what the 3 part parameter "class CString filename" would mean. The code: int TestFunc(class CString filename) { fopen(filename, "w"); } Compile using Visaul C++,...
4
by: Susan Rice | last post by:
I'm new to using CString. Why won't the following compile? I'm using Microsoft Visual C++ 6.0 Line 37 which it complains about is the function: 37 CString ConvertFile(char *szFileName) I...
2
by: flyingxu | last post by:
Hi, I run into a cstring related link problem in VC7. My solution has 3 projects, one MFC exe, two MFC extersion DLL. the two MFC extersion DLL export functions which use CString as parameters....
9
by: Donos | last post by:
I have a CString with 100 characters. Now i want to make that to 2 lines. For example, CString str = "This is just a test to find out how to break a cstring"; I want this CString in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.