What am i doing wrong?

This is a discussion on What am i doing wrong? within the C++ Programming forums, part of the General Programming Boards category; This is a program that reads data from a disk and outputs data to the same disk. The data is: ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    Question What am i doing wrong?

    This is a program that reads data from a disk and outputs data to the same disk.
    The data is:
    @2,89#3,*67
    $187,3#34,72#123#
    *3*7*1#*3,4,8

    The program must exclude the symbols & just output the digits with the '#' starting a new line.

    Output should be:
    289
    367
    1873
    3472
    123
    371
    348

    Code:
    c++
    #include <fstream.h>
    #include <iostream.h>
    #include <cctype>

    using namespace std; //introduces namespace std

    char const nwln = '\n';

    int main ( )
    {
    char ch;
    ifstream data;
    ofstream dataout;
    int n;
    int sum;

    data.open ("a.\\data.txt");
    if (!data)
    {
    cout << "ERROR -- Cannot open data.txt for input" << end1;
    return 1;
    }

    dataout.open ("a:\\dataout.txt");
    if (!dataout)
    {
    cout << "ERROR -- Cannot open dataout.txt for output" << end1;
    return 1;
    }


    data.get(ch);
    while (data)
    { sum = 0;
    cdigit = 0;
    while ((ch != '\n') && data)
    {
    if (isdigit(ch))

    cdigit = ch - '0';
    sum = (sum*10) + n
    dataout.put(ch);
    data.get(ch);

    }
    if (ch == '#')
    dataout << sum << end1;
    sum = 0;
    }

    }
    data.close(); dataout.close();
    return 0;
    }

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    This should work:
    Code:
    while (data.get(c)) {
      if (c == '#')
        dataout<<'\n';
      if (!isdigit(c)
        continue;
      dataout<< c;
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    grrrr
    Code:
    #include <fstream>
    #include <iostream>
    #include <cctype>
    
    using namespace std; //introduces namespace std
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Code:
    c++


    You have got to be kidding.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    PHP has a great function for this type of thing, explode. But, if you are using C++ you will probably have to use strtok. google it.

  6. #6
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >But, if you are using C++
    I'd say that's a fair assumption seeing as how the web site is called Cprogramming.com and the forum being posted in is called C++ Programming. Not to mention that the original post including quite a bit of C++ source.

    >you will probably have to use strtok.
    strtok breaks up a string based on delimiters you pass to it. If you include all of the non-digit characters, strtok will split up the string too much. If you only include the '#' as a delimiter to strtok, you still have to deal with the other special characters. Conclusion: strtok doesn't solve the problem well enough to be worth using it. And of course there's the fact that the original post didn't say anything about using strings, just character by character stream input.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    true, just suggestions.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21