The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 04-26-2008, 08:32 AM
kckc314 kckc314 is offline
Novice
Join Date: Apr 2008
Posts: 14
iTrader: (0)
kckc314 is on a distinguished road
char data type

i am going to develop a program will read data from a file that contains #s, simple spaces, digits and new line characters. in the program i am going to use while ((ch = getchar()) != EOF). when i declare the ch, it must be in type char or i can use type int? due to the digits part eventually i need to store them into 2-d array and use them as a number for operation. please advise what is the best and simple way to get on. thanks in advance.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 04-27-2008, 12:58 AM
kckc314 kckc314 is offline
Novice
Join Date: Apr 2008
Posts: 14
iTrader: (0)
kckc314 is on a distinguished road
hi can i have some feedbacks?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 04-27-2008, 02:57 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
See if you understand this example:

Code:
// io/read-file-sum.cpp - Read integers from file and print sum.
// Fred Swartz 2003-08-20

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main() {
    int sum = 0;
    int x;
    ifstream inFile;
    
    inFile.open("test.txt");
    if (!inFile) {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }
    
    while (inFile >> x) {
        sum = sum + x;
    }
    
    inFile.close();
    cout << "Sum = " << sum << endl; 
    return 0;
}

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 04-27-2008, 03:41 AM
kckc314 kckc314 is offline
Novice
Join Date: Apr 2008
Posts: 14
iTrader: (0)
kckc314 is on a distinguished road
well, the code was trying to open a file called test.txt. if it opens successfully, then it copies all the contents from the file to the variable x. variable sum then holds the number of character being copied and display the number of characters. the sum and x variable declare as type int, but in the file test.txt does it contain all kinds of character?

that means i can declare variable ch as type int, according to my question above, if you output to type int variable, it can also work for input to type int variable. am i correct? but as i mentioned my test.txt apart from digit numbers, it has '#' character, simple spaces and new line character. as type int variable still can work with these characters is that right?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 04-27-2008, 12:41 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 303
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
You could if they was represented as ASCII or any other char code that just used numbers.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply

« Array | void »

Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:00 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50