PDA

View Full Version : c ++ help needed




mikeyredk
Sep 9, 2003, 07:51 AM
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 (https://cms.psu.edu/Profile/FileManager/default.asp?WCI=pgTransferFile&WCU=FILEMAN&FILE=STARLOOP%2ECPP&DIR=&saveas=STARLOOP.CPP)



caveman_uk
Sep 9, 2003, 08:03 AM
I got a session timeout following the link. I think you need to put it somewhere more publicly accessible.:rolleyes:

mikeyredk
Sep 9, 2003, 08:35 AM
okay let me just post it don't know why the link doesn't work its my website that penn state gives me

// 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()

bousozoku
Sep 9, 2003, 09:12 AM
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.

mikeyredk
Sep 9, 2003, 09:30 AM
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

Jeffrey Lim
Sep 9, 2003, 09:41 AM
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)?"

msp
Sep 9, 2003, 09:44 AM
[i]Originally posted by mikeyredk

_ _ while (ans !='Y' && ans != 'N');

[/B]

Your error is here :


while (ans != 'Y' || ans != 'N')


A good irc channel for c++ is #c++ on irc.debian.org

caveman_uk
Sep 9, 2003, 09:47 AM
Originally posted by mikeyredk
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
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?

caveman_uk
Sep 9, 2003, 09:54 AM
Originally posted by msp
Your error is here :


while (ans != 'Y' || ans != 'N')


A good irc channel for c++ is #c++ on irc.debian.org
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....

mikeyredk
Sep 9, 2003, 10:02 AM
Originally posted by Jeffrey Lim
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)?"

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

mikeyredk
Sep 9, 2003, 10:04 AM
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

caveman_uk
Sep 9, 2003, 10:08 AM
Isn't this something to do with declaring a namespace???? It's been a loooooong time since I did any C++

bousozoku
Sep 9, 2003, 02:00 PM
Originally posted by mikeyredk
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

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

mikeyredk
Sep 20, 2003, 09:42 AM
okay i would like to know how i would be able to acess a trace function in project builder or xcode

saabmp3
Sep 21, 2003, 02:38 AM
.h is the standard naming convention for header files unless you are using M$ specific crap.

BEN

mikeyredk
Sep 21, 2003, 03:50 PM
anyone with a real answer to my question