Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikeyredk

macrumors 65816
Original poster
Mar 13, 2003
1,267
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
 
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()
 
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.
 
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
 
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)?"
 
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?
 
Originally posted by msp
Your error is here :

Code:
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....
 
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
 
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
 
Re: very odd

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.
 
more help needed

okay i would like to know how i would be able to acess a trace function in project builder or xcode
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.