473,403 Members | 2,366 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,403 software developers and data experts.

Desperate help with C++ project

Ok, so I have a project that I need to do with C++. I have finished all of it, but one part. One of the fuctions (filling the array) needs to open a file for reading ( (7,16,43,23,53) etc) and output it to an arry of ints. I can get the first one to read in fine, but then it seems to ignore the rest. I suspect it involves the comma because when I replace them with spaces it works perfect. The issue is that I have to keep everything the way it is, just remove the commas. I am at a loss as to how and have tried so many things. I am not that good with C++ and I could really use some help. Code is below, anything offered would be such a great help.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void FillMyInput();
  9. void FillMyArray( int arr[], int SIZE );
  10. int GetMax( int arr[], int SIZE );
  11. void OutputArray( int arr[], int SIZE );
  12.  
  13. const int SIZE = 10;
  14.  
  15. int main(int argc, char* argv[])
  16. {
  17.     int arr[ SIZE ];
  18.  
  19.     FillMyInput();
  20.  
  21.     FillMyArray( arr, SIZE );
  22.  
  23.     GetMax( arr, SIZE );
  24.  
  25.     OutputArray( arr, SIZE );
  26.  
  27.     return 0;
  28. }
  29.  
  30. void FillMyInput()
  31. {
  32.     ofstream fillFile;
  33.     fillFile.open( "myInput.txt" );
  34.     fillFile << "7,64,15,12,3,78,23,14,45,1";
  35.     fillFile.close();
  36.     cout << "Filled file with text" << endl;
  37. }
  38.  
  39. void FillMyArray( int arr[], int SIZE )
  40. {
  41.     ifstream arrayFile;
  42.     arrayFile.open( "myInput.txt" );
  43.     for( int i = 0; i < SIZE; i++ )
  44.         arrayFile >> arr[i];
  45.         cout << "done\n";
  46.     cout << endl;
  47.     arrayFile.close();
  48.     cout << "Filled arrary with file contents" <<endl;
  49. }
  50.  
  51. int GetMax( int arr[], int SIZE )
  52. {
  53.     char c;
  54.     int maxNum = 0;
  55.     for( int i = 0; i < SIZE; i++ )
  56.     {
  57.         if (arr[i] > maxNum)
  58.             maxNum = arr[i];
  59.     }
  60.     cout << "\nThe largest number in the array was " << maxNum <<endl; 
  61.     return maxNum;
  62.  
  63. }
  64.  
  65. void OutputArray( int arr[], int SIZE )
  66. {
  67.     cout << "\nArray Outputs\n" << endl;
  68.     for( int i = 0; i < SIZE; i++ )
  69.         cout << arr[i] << endl;
  70.     cout << endl;
  71. }
  72.  
  73.  
  74.  
Feb 5 '08 #1
2 1196
Hi,

Try this way

#DEFINE C_DELIMETER_COMMA ","

char *p_szToken = '\0';
p_szToken = strtok ( m_szSource, C_DELIMETER_COMMA );
while( p_szToken != NULL )
{
// read the values from p_szToken
p_szToken = strtok( NULL, C_DELIMETER_COMMA );
}

I think this might help you

Ganesh
Feb 5 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Do not use strtok() in C++. The entire C string library is deprecated in C++.

If you have ints like this:
1,2,3,4,5,6

Then I suggest you read them in by:

Expand|Select|Wrap|Line Numbers
  1. int arr[6];
  2. char comma;
  3.  
  4. arrayFile >> arr[i] >> comma;
  5.  
That is, eat the comma by >> to a char.
Feb 5 '08 #3

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

Similar topics

1
by: Luke Dubbber | last post by:
I am currently new to PHP and I am reading a book by Premier Press called PHP Professional Projects. I am new to PHP, I heard it was better and faster and way more flexible so far they have been...
1
by: JatP | last post by:
hi Everyone I am trying to create a server and client to send files from one side to the other. I can send files from one side to the other using bufferedinput/output streams but when trying to...
3
by: Kevin | last post by:
I just installed WinXP Pro SP1. I installed IIS 5.1. I have a project that was created in VB6 using webclasses (yuck!). It will not run. Here is the problem: If I do this: Set...
1
by: wolfiecat | last post by:
Hello - I am new to this forum. Let me start off by saying that I have never used ASP before. I am working on a project where the site was already created using frames and .aspx pages. I was...
2
by: lawrence | last post by:
We've developed a content management system that we donate to the public domain (via a Creative Commons declaration) at www.publicdomainsoftware.org. Our cms has an emphasis on weblogs, though we...
2
by: Chris M. Gamble | last post by:
I am trying to perform what I best understand as Multi-master asynchronous replication for postgres 7.3.3 servers. After researching, I tried the pgReplication project (and made the simple...
2
by: phantom | last post by:
Hi, I recently inherited an online application that was written by someone who is no longer with our company. He said that he was doing it in ASP.Net but the code is actually in C#. I am a vb...
5
by: beachlounger | last post by:
What the heck is wrong with this: #ifndef POLYNOMIAL_H #define POLYNOMIAL_H class Polynomial { public: Polynomial(); Polynomial operator+( const Polynomial& ) const; // addition
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.