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. -
#include "stdafx.h"
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
-
using namespace std;
-
-
void FillMyInput();
-
void FillMyArray( int arr[], int SIZE );
-
int GetMax( int arr[], int SIZE );
-
void OutputArray( int arr[], int SIZE );
-
-
const int SIZE = 10;
-
-
int main(int argc, char* argv[])
-
{
-
int arr[ SIZE ];
-
-
FillMyInput();
-
-
FillMyArray( arr, SIZE );
-
-
GetMax( arr, SIZE );
-
-
OutputArray( arr, SIZE );
-
-
return 0;
-
}
-
-
void FillMyInput()
-
{
-
ofstream fillFile;
-
fillFile.open( "myInput.txt" );
-
fillFile << "7,64,15,12,3,78,23,14,45,1";
-
fillFile.close();
-
cout << "Filled file with text" << endl;
-
}
-
-
void FillMyArray( int arr[], int SIZE )
-
{
-
ifstream arrayFile;
-
arrayFile.open( "myInput.txt" );
-
for( int i = 0; i < SIZE; i++ )
-
arrayFile >> arr[i];
-
cout << "done\n";
-
cout << endl;
-
arrayFile.close();
-
cout << "Filled arrary with file contents" <<endl;
-
}
-
-
int GetMax( int arr[], int SIZE )
-
{
-
char c;
-
int maxNum = 0;
-
for( int i = 0; i < SIZE; i++ )
-
{
-
if (arr[i] > maxNum)
-
maxNum = arr[i];
-
}
-
cout << "\nThe largest number in the array was " << maxNum <<endl;
-
return maxNum;
-
-
}
-
-
void OutputArray( int arr[], int SIZE )
-
{
-
cout << "\nArray Outputs\n" << endl;
-
for( int i = 0; i < SIZE; i++ )
-
cout << arr[i] << endl;
-
cout << endl;
-
}
-
-
-
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
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: -
int arr[6];
-
char comma;
-
-
arrayFile >> arr[i] >> comma;
-
That is, eat the comma by >> to a char.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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,...
|
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...
| |