c ++ help needed

Discussion in 'Mac Apps and Mac App Store' started by mikeyredk, Sep 9, 2003.

  1. mikeyredk macrumors 65816

    Joined:
    Mar 13, 2003
    #1
    i have some code from my computer science class that won't work well it will run and all but it does something really really dumb
    i already broke the program down and don't know whats up

    the code here
     
  2. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    I got a session timeout following the link. I think you need to put it somewhere more publicly accessible.:rolleyes:
     
  3. mikeyredk thread starter macrumors 65816

    Joined:
    Mar 13, 2003
    #3
    okay let me just post it don't know why the link doesn't work its my website that penn state gives me
    Code:
    // includes
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <ctype.h>
    #include <math.h>
    #include <string.h>
    #include <alloc.h>
    #include <fstream.h>
    #include <iomanip.h>
    // prototypes
    double cinFloat();
    // defines
    #define SKIPLINE cout<<endl
    
    ///////////////////////////////////////////////////////////////////
    // _Programmer:
    // _ _Date Due:
    // _ _ _ Input:
    // _ _ Process:
    // _ _ _Output:
    //
    int main(){
    //declarations
     _char
    	 ans;			//eof check
    //heading
    
    //main loop
     _do{
     _ _ //input
    
    	 _//<<<process>>>
    
     _ _ //output
    
     _ _ //check for exit
     _ _ cout<<endl<<"continue? (Y/N) ";
     _ _ do
     _ _ _ ans = toupper(getchar());
     _ _ while (ans !='Y' && ans != 'N');
     _ _ SKIPLINE;
     _}while (ans == 'Y');
     _SKIPLINE;
    //wrap up
     _cout<<endl<<"End of program";
    return (0);
    }//main()
    //-------------------------------------------------------
    double cinFloat(){
     _char* s = new char[30];
     _cin>>s;
     _double x = atof(s);
     _delete [] s;
     _return x;
    }//cinFloat()
    
    
     
  4. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    It just looks to me that it asks you multiple times if you want to continue.

    You've got a function to get some characters and turn them into a float but you don't access it from what I can see. You have to call cinFloat() in the main function, if you want to use it.
     
  5. mikeyredk thread starter macrumors 65816

    Joined:
    Mar 13, 2003
    #5
    maybe i should have said what the prob is

    in the run display i don't see the question why is that it looks like the second dowhile is messing up the output because when i remove the second dowhile it actually outputs the question do you want to continue y/n
     
  6. Jeffrey Lim macrumors newbie

    Joined:
    Sep 9, 2003
    #6
    Well, the actual problem is that the display isn't "flushing".

    Many unix systems do not actually 'transfer' output to the screen/terminal until they reach an end-of-line.

    So doing:

    cout << "Hello";

    will produce nothing on the screen

    But

    cout << "Hello" << endl;

    will show what you'd expect.

    So the quick answer is that you need to flush the display after prompting for "Continue (Y/N)?"
     
  7. msp macrumors newbie

    Joined:
    May 6, 2003
    Location:
    Karlsruhe, Germany
    #7


    Your error is here :

    Code:
    while (ans != 'Y' || ans != 'N')
    
    A good irc channel for c++ is #c++ on irc.debian.org
     
  8. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #8
    When you say 'remove the second do while' do you mean just the while and do lines or the ans=... line as well? If you just remove the do/while lines and leave the ans... line what happens?
     
  9. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #9
    Doesn't he want AND not OR? He wants a response that is 'Y' or 'N' so he has to repeat whilst it it isn't 'Y' and it isn't 'N'

    Because if it IS 'Y' it will still !='N' therefore an || will still be true and he will not exit the loop

    I think it's the flushing theory....
     
  10. mikeyredk thread starter macrumors 65816

    Joined:
    Mar 13, 2003
    #10
    that did it
    this loop is just a practice loop before we actually start putting stuff inside it so i wanted to see if i can get the loop started

    msp thx for the irc channel

    thx for your help this might not be the last time i post in this thread hopefully
     
  11. mikeyredk thread starter macrumors 65816

    Joined:
    Mar 13, 2003
    #11
    very odd

    okay just noticed something the program isn't warning me about the "cout"
    when i did some easier programs i had to use "std::cout" but now it lets me use "cout"
    also "<<endl" it use to tell me i had to intilize it before use now it's letting me use it
     
  12. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #12
    Isn't this something to do with declaring a namespace???? It's been a loooooong time since I did any C++
     
  13. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #13
    Re: very odd

    It probably has something to do with the fact that you're declaring your header files with the non-standard .h file names.
     
  14. mikeyredk thread starter macrumors 65816

    Joined:
    Mar 13, 2003
    #14
    more help needed

    okay i would like to know how i would be able to acess a trace function in project builder or xcode
     
  15. saabmp3 macrumors 6502a

    Joined:
    Jul 22, 2002
    Location:
    Tacoma, WA
    #15
    .h is the standard naming convention for header files unless you are using M$ specific crap.

    BEN
     
  16. mikeyredk thread starter macrumors 65816

    Joined:
    Mar 13, 2003

Share This Page