help me help myself

This is a discussion on help me help myself within the C++ Programming forums, part of the General Programming Boards category; Hello everyone..... I've learned a lot in the last few days, thanks to all of you and the tutorials on ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    help me help myself

    Hello everyone.....

    I've learned a lot in the last few days, thanks to all of you and the tutorials on this site. Thanks most sincerely.

    So.... what I've learned so far is how to write a half-functioning/half-not-functioning data entry program using arrays. I'm getting it... I'm almost there.... My question is this:

    I'm getting a weird number appearing on the line after the data entry. It's the same number every time : 1244532

    Obviously I've done something wrong. Is anyone willing to give me a hint? Loop weirdness? Some sort of missing statement after the data entry? Gremlins?

    Also... sort of the same subject... I seem to be bracket impaired... are there any handy tips for figuring out where they need to be placed and proper indentation thereof?

    Code:
    #include <conio.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <iostream.h>
    #include <math.h>
    
    ofstream fout;
    
    int main ()
      {
        // variable declarations
        double array [65];
        int i, j, index, data;
    
        // alphabetized variable dictionary
        //  i is vertical spacing
        //  j is horizontal spacing
        //==============================================================
    
        fout.open ("prompt.txt");
    
        // this is a program to store data in a single array
       cout << setw (50) << "  " << endl;
       cout << endl << endl << endl << endl << endl << endl << endl;
       cout << setw (50) << "Storing Data in an Array" << endl;
       cout << endl << endl << endl << endl << endl << endl << endl << endl;
    
          // press any key to continue
          cout << setw (50) << "Press return to continue" << endl;
          getchar();
          clrscr ();
    
    //==============================================================
        // run program until user is done
    
    
        
        do
        {
          // set initial values to 0
          data = 0;
          index = 0;
    
          // prompt the user for the next data
          cout << endl << endl << endl << endl<< endl << endl << endl << endl;
          cout << setw (60) << "Enter -1 for end of data; Enter next data # " << data +1  << ": ";
            cin >> array [index];
    
          // display data entered
          cout << setw (38) << " : " << array << endl;
          index = index +1;
          data = data +1;
          cout << endl << endl << endl << endl << endl << endl;
          getchar();
    
          }
        while (array [index -1]!= -1);
              // ask the user to verify accuracy of data and to correct any errors
              //{
              cout << setw (60) << "Enter index of incorrect data; Enter 0 for none: " << data << ": ";
              //}
                  
        fout.close ();
        return 0;
      }

  2. #2
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    First, don't #include <conio.h>, as it's not standard header. And quit using clrscr(). You need to use
    Code:
    #include <fstream>
    #include <iomanip>
    #include <iostream>
    #include <math.h>
    2nd,
    Code:
    // display data entered
          cout << setw (38) << " : " << array[index] << endl;
    (you're missing the index of the array)
    As you do
    Code:
    do
        {
          // set initial values to 0
          data = 0;
          index = 0;
          ............
          cin >> array [index];
          ............
          }
    while (array [index -1]!= -1);
    all you do is in fact you initialize only the first element of the array. You should comment the line
    Code:
          index = 0;
    , and add a control for not getting more than 65 elements from the user (double array [65];)
    Also, you declared i and j variables, but you never use them.
    And help me help myself it's not a good title for a thread...
    Last edited by frrossk; 02-18-2005 at 01:51 AM.

  3. #3
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    Missing first time...What are you using that file (fout.open ("prompt.txt");) for???

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    The answer to all your "why are you doing this?" questions are:

    Wish I knew. It's the dictionary and language the professor has demanded we use, in spite of the fact that many others suggest that it's not the best way to go.

    But if you met her, you'd just type it too.

    However... what do you suggest as an alterntive to clscr ()?

    fyi .. the i and j integers are there for some pretty-fying work I need to do after I get it to run cleanly.

  5. #5
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    You have 2 lines of code:
    Code:
    ......................
    cout << endl << endl << endl << endl << endl << endl << endl;
    ......................
    cout << endl << endl << endl << endl<< endl << endl << endl << endl;
    that in my console do almost the same thing as clrscr().....

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by frrossk
    You have 2 lines of code:
    Code:
    ......................
    cout << endl << endl << endl << endl << endl << endl << endl;
    ......................
    cout << endl << endl << endl << endl<< endl << endl << endl << endl;
    that in my console do almost the same thing as clrscr().....
    is there really a need to flush the buffer that many times? a better way to do it is
    Code:
    std::cout<<"\n\n\n\n\n\n\n"<<std::flush;
    actually, the best thing to do is not to have to clear the screen at all... and it doesn't do the same thing as clrscr(), because clrscr() will actually clear the screen and start you printing back at the top. your way will have you printing at the bottom.

    basically, if your compiler has clrscr(), go ahead and use it, but just know that your code is losing alot of it's portability. a better way is to use a function or macro. for example, a funciton would look like:
    Code:
    #include<cstdlib>
    
    inline void clearScreen() { system("cls"); }
    
    int main() {...}
    that way, if your code is otherwise standard C/C++, with just a single change you can port your code between operating systems. this also opens you up to a host of problems with using system(), but it should be fine if you're just doing it in a classroom environment.

    also, you'll want to include <cmath> instead of <math.h>, and either put std:: in front of every cout, cin, endl, etc. that you use, or just put using namespace::std; under your #includes. of course, if your compiler doesn't like <iostream>, don't worry about it for now. just know that your compiler is outdated.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    >>and it doesn't do the same thing as clrscr(), because clrscr() will actually clear the screen and start you printing back at the top. your way will have you printing at the bottom.
    I know; I was talking only about what I see on the screen, without scrolling...
    >>basically, if your compiler has clrscr(), go ahead and use it, but just know that your code is losing alot of it's portability
    You're right; but I was thinking that <conio.h> (also clrscr) it's not a standard header...

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Also... sort of the same subject... I seem to be bracket impaired... are there any handy tips for figuring out where they need to be placed and proper indentation thereof?
    Code:
    int main()
    { 
    .    int a = 10;
    .    
    . if(a==10)
    . {
    .        cout<<"a: "<<a<<endl;
    . }
    .    else
    .    {
    .       cout<<"mistake"<<endl;
    .    }
    .
    .    return 0;
    }
    The braces should line up under the first letter of the block header. If you get a good text editor, the brackets will be handled automatically for you. When you type, they will be indented the proper amount and line up correctly.
    Last edited by 7stud; 02-18-2005 at 10:51 AM.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > are there any handy tips for figuring out where they need to be placed and proper indentation thereof?
    Yeah, when you type the opening brace, bracket, quote, etc, type in the matching closing brace, bracket, quote.

    That way, they're always balanced and you don't have to go through potentially masses of code trying to figure out where the extra one should go.

    Example
    Code:
    if ( ) {
    } else {
    }
    That's how I write an if/else to start with.
    Then I fill it in with whatever I need.

    Everything is always balanced, and everything remains nicely formatted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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