LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-13-2004, 07:16 AM   #1
Dimitris
LQ Newbie
 
Registered: Aug 2003
Posts: 13

Rep: Reputation: 0
Lightbulb Char array to int without losing value ?


Hello,

I wrote some simple code for practice (not for school/college exercise etc.) and have a problem which I have founded but don't know how to fix it. I read number from txt file. The first number is the number of the numbers following. A sample of that file is:
4
12
45
2
1

Number can go up to 100 and must be at least three(except the first one which is the number of the numbers following).

What I want is to read the file and put numbers into an array so that I can then compare them etc.
The problem is that they are char and not int.

Take a look at the code please.

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
ifstream file("INPUT.TXT");

char record;
char trecord[100];
char temp;
char j;
int i=0;

while(!file.eof())
{
file.get(record);
file.get();
trecord[i]=record;
i++;
}

file.close();

for (j=trecord[0]; j<=2; j--)
{

if (trecord[j]<=trecord[j-1])
{
temp=trecord[j-1];
trecord[j-1]=trecord[j];
trecord[j]=temp;
}
cout<<"\n"<<trecord[j];
}


return 0;
}

I hope you understand my problem and can help me(if you don't please ask me for more info).
Please give me a tip or a suggested code for some help.
Thanks alot,

Have a nice day!
 
Old 01-13-2004, 08:06 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
ok, so youve got an istream, so why dont you read an integer from it

Code:
int i;

while(!file.eof()) {
    file >> i;
    cout << i;
}
in a production application its better to read a string from the file then setup an istringstream on the string to extract the number rather than extracting directly from the file but if you know the file will be in the right format then it doesnt matter
 
Old 01-13-2004, 06:33 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
You can also use atoi(record) to convert the string to an integer.

trecord[i] = atoi(record);
 
Old 01-14-2004, 12:08 PM   #4
Dimitris
LQ Newbie
 
Registered: Aug 2003
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks alot for your answers,

I solved my problem for now.

Thanks to both of you.

Have a nice day!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
char to int: a twist Guru3 Programming 5 12-05-2004 08:49 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
int(char(254)) becomes kalleanka Programming 2 02-17-2004 03:35 PM
int, long in char * marek Programming 10 09-01-2003 10:43 AM


All times are GMT -5. The time now is 09:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration