|
 |
|
 |
10-08-2004, 03:56 PM
|
#1 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
someone from mac underground needs help
this is a direct "copy and past" of what the person needs help on. i can't help him, i hoping one of you could.
Quote:
i'm stuck in my third project for my c++ class. it's a saturday class, so you miss one of em and your already lost when you come back. so anyway this project is a console app that will ask the user to input numbers (positive or negative) and display all sorts of averages and what not. that part is easy. the part that i don't understand is to get out of the input section, the user is supposed to hit ctrl-z. this will then display all the averages etc. i don't know what ctrl-z is. is it a breaker? is it something in a library i have to include? is it something i myself have to code? and if so? how do i do that? because the cin is set to integer type. and wouldn't ctrl-z be counted as cin as well? if any of this is unclear i can send anyone willing to help me the example app.
one thing that really pisses me off is that the example apps that my teacher sends us has bugs in them sometimes. like one part in my last project was supposed to ask a user where they are from;
cout << "Where are you from?";
cin >> answer;
cout << "If you lived in germany your name would be " << name << " von " << answer;
but see this doesnt work, because if the person comes from a city with two words in it's name like "New York". the space between cuts off and the rest is truncated. so i had to use getline instead. he didnt use getline in his example. and i'm suppossed to follow these faulty progs? argh!
so anyway thanks in advance for any help. ciao
ps using mingw studio
|
|
|
|
10-09-2004, 12:49 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
|
Isn't this supposed to be ^D (ctrl+d)? which means EOF, as I understand (ctrl+z) will suspend any program (atleast on *nix it does), There's no abitrary char value for it as far as I remember.
(ctrl+s) will pause any output from a console, atleast on *nix it does, and then you can use (ctrl+q) to reactivate it.
But (ctrl+z) I've never heard of it apart from the "Undo" command in some windows progs.
If he wanted to find out, he could make a small program to test it ie:
Code:
#include <stdio.h>
#define BUFFER_SIZE 10
int main(){
while(!feof(stdin))
{
char buffer[BUFFER_SIZE+1];
int n;
if(!fgets(buffer, BUFFER_SIZE, stdin))
break;
for(n=0; buffer[n] != '\0' && buffer[n] != '\n'; n++)
printf("Int value: %d\n", (int) buffer[n]);
}
return 0;
}
Warning code hasn't been tested.
Just to print, what integer value it might give..
Then he could use that integer in the comparison when dealing with (ctrl+z).
|
|
|
10-09-2004, 04:21 AM
|
#3 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
it says he's using mingw studio
|
|
|
10-09-2004, 04:39 AM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
get out of the input section, the user is supposed to hit ctrl-z. this will then display all the averages
|
This is ancient. And bad design as well. I'll explain.
CTRL^Z was needed in times when there was a switch from cp/m machines to dos. File input needed to terminate with ^z in those days. The ctrl^z mechanism was introduced in dos for compatibility with cp/m systems. It has to do with how files were stored those days: not in bytes, but in sectors. I've forgotten the details long time ago.
Quote:
|
get out of the input section
|
There are tons of ways to deal wtih "menu" issues. Yet he manages (the teacher) to concentrate on a plain stupid method.
If you like it, I can build you some app that does ask for input and calculates the average. What is your current level or skill set?
What are the further requirements, if any?
__________________
|
|
|
10-09-2004, 09:09 AM
|
#5 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
i'll tell him what you said and wait for a reply
|
|
|
10-09-2004, 01:45 PM
|
#6 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
heres an update
Quote:
|
yeah, i don't need someone to build me an app, i was just wondering how to code the ctrl-z part. that's the part that got me.
|
|
|
|
10-10-2004, 05:45 AM
|
#7 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
i think i found the answer to his problem....
1) On UNIX systems, when stdin is redirected from a terminal, Ctrl-D is
interpreted as EOF only when it is issued at the beginning of the line.
Otherwise, it is interpreted literally. Did you issue Ctrl-D immediately
after the prompt?
2) On Windows systems, when stdin is redirected to the keyboard, Ctrl-Z
marks EOF only when followed by ENTER. Have you hit ENTER after Ctrl-Z?
|
|
|
10-10-2004, 06:54 AM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
quote:
--------------------------------------------------------------------------------
yeah, i don't need someone to build me an app, i was just wondering how to code the ctrl-z part. that's the part that got me.
--------------------------------------------------------------------------------
|
Here's a lil surprise for your friend (and you). Run it and see what conclusions you can draw:
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Elementary functions(s)
void requestValue();
//Optional helper function(s)
void wait_for_enter();
//Elementary globals.
vector<double> theVector;
double theVal;
int main()
{
requestValue();
//Process data here.
wait_for_enter();
return 0;
}
//////////////////////////////////////////////////////////////
void requestValue()
{
cout<<"Enter the value (double precision accepted) => ";
cin>>theVal;
while(!cin.fail() )
{
theVector.push_back(theVal);
cout<<"Enter the next value => ";
cin>>theVal;
}
}
//////////////////////////////////////////////////////////////
void wait_for_enter()
{
cout << "press <enter> to continue...\n";
// Reset failstate, just in case.
cin.clear();
string line;
getline( cin, line);
}
Obviously requestValue() is what your friend is interested in.
__________________
|
|
|
10-10-2004, 07:48 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
|
Ahhh.. the joy of STL <vector> types and it's push() and pop()...
|
|
|
10-11-2004, 02:16 AM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
*bump*
Uhm Androto do you realize I gave you the basic solution for your friend?
__________________
|
|
|
10-11-2004, 04:47 AM
|
#11 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
yes, but he hasn't responded yet, so i didn't post anything here.
ps. what does *bump* mean?
|
|
|
10-11-2004, 05:34 AM
|
#12 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
The famous *bump*
Catching attention.

__________________
|
|
|
10-11-2004, 08:13 AM
|
#13 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
ohhhhhh
|
|
|
10-12-2004, 05:20 PM
|
#14 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
Quote:
k, here's what i got. it's not working. it just halts after i press ctrl-z. i'm sure there are way better ways of figuring this out, don't mind my bloated code. not sure if i want to have the computations in the loop, so i commented out per and avg. and yes i did find out how to do the ctrl-z. it's the (!cin.eof()) part. not really sure if it's working though. so yeah, anyway when i run this and type a few numbers in...it just halts after i press ctrl-z anyone know what i'm doing wrong?
Code:
#include <iostream>
using namespace std;
int main()
{
int input, sum = 0, rem, oddsum = 0, negsum = 0, possum = 0, total = 0;
float per, avg;
cout << "Enter an integer or ctrl-z to exit: ";
cin >> input;
while (!cin.eof()) {
cin >> input; //user input
while (input != 0) {
cin >> input; //user input
total++; //increment total by 1
if (input < 0) { //find out negative integers
negsum++; //increment negsum by 1
//per = negsum / total; //compute percent of negative numbers
}//end of if
else { //find out positive integers
rem = input % 2;
if (rem = 0) { //find out even integers
sum += input;
}//end of if
else { //find out odd integers
possum++;
oddsum += input;
//avg = oddsum / possum; //compute average of odd positive integers
}//end of else
}//end of else
}//end of while
}//end of while
per = negsum / total;
avg = oddsum / possum;
cout << "Here is the stats of the " << total << " numbers you entered:" << endl;
cout << per << " of the numbers were negative." << endl;
cout << "The sum of the positive even integers is " << sum << endl;
cout << "The average of the positive odd integers is " << avg << endl;
}
|
another update
|
|
|
10-12-2004, 05:33 PM
|
#15 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
ctrl-z. it's the (!cin.eof()) part
|
Yes. cin.fail() it is. And cin.eof(). It depends on the situation, like what kind of tests you want to perform. I'll get come code up soon.
__________________
Last edited by Valmont; 10-13-2004 at 08:47 AM.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:36 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|