Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 10-08-2004, 03:56 PM   #1 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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
Androto is offline   Reply With Quote
Old 10-09-2004, 12:49 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
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).
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-09-2004, 04:21 AM   #3 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
it says he's using mingw studio
Androto is offline   Reply With Quote
Old 10-09-2004, 04:39 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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?
__________________
Valmont is offline   Reply With Quote
Old 10-09-2004, 09:09 AM   #5 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
i'll tell him what you said and wait for a reply
Androto is offline   Reply With Quote
Old 10-09-2004, 01:45 PM   #6 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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.
Androto is offline   Reply With Quote
Old 10-10-2004, 05:45 AM   #7 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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?
Androto is offline   Reply With Quote
Old 10-10-2004, 06:54 AM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 10-10-2004, 07:48 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
Ahhh.. the joy of STL <vector> types and it's push() and pop()...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-11-2004, 02:16 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
*bump*

Uhm Androto do you realize I gave you the basic solution for your friend?
__________________
Valmont is offline   Reply With Quote
Old 10-11-2004, 04:47 AM   #11 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
yes, but he hasn't responded yet, so i didn't post anything here.


ps. what does *bump* mean?
Androto is offline   Reply With Quote
Old 10-11-2004, 05:34 AM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
The famous *bump*
Catching attention.

__________________
Valmont is offline   Reply With Quote
Old 10-11-2004, 08:13 AM   #13 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
ohhhhhh
Androto is offline   Reply With Quote
Old 10-12-2004, 05:20 PM   #14 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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
Androto is offline   Reply With Quote
Old 10-12-2004, 05:33 PM   #15 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MAC address iwanttolearnc Platform/API C++ 0 07-27-2004 10:46 PM
install Mono on Mac OS X mcjules MS Technologies ( ASP, VB, C#, .NET ) 2 06-25-2004 10:28 AM
MAC new line? Admin PHP 8 01-13-2004 08:38 PM
mac vs pc ??? sde Lounge 7 10-01-2003 09:09 AM
How about a section on Programming on the Mac. phpjeff Feedback 5 05-08-2002 12:36 PM


All times are GMT -8. The time now is 10:36 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting