Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 4th, 2004, 07:51 PM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default why it jump?

This is a example I got from website. I run it, the output is weird,
can anybody tell me the reason?
Thanks!


#include <iostream.h>
#include <string.h>

 struct students student_info(struct students data);
 void print(struct students data);

 #define MAX_STU 5

 struct students {
 char name[30];
 int age;
 char grade[2];
 int iq;
 };


 main() {

 students student[MAX_STU];
 int i;

 //get the data
 for (i=1; i<MAX_STU; ++i) {
 student[i] = student_info(student[i]);
 }

 //print the data
 for (i=1; i<=MAX_STU; ++i) {
 print(student[i]);
 }
 }

 struct students student_info(struct students data) {
 cout << "what is the students name: ";
 cin.getline (data.name, 30);
 cout << "What is the students age: ";
 cin >> data.age;
 cout << "What is the students grade: ";
 cin.getline (data.grade, 2);
 cout << "What is the students IQ: ";
 cin >> data.iq;
 return (data);
 }


 void print(struct students data) {
 cout << "Name: " << data.name << endl;
 cout << "Age: " << data.age << endl;
 cout << "Grade: " << data.grade << endl;
 cout << "IQ: " << data.iq << endl << endl;
 }

Reply With Quote
  #2 (permalink)  
Old November 7th, 2004, 02:48 PM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

NO one knows the reason? or everbody here just think it's too easy?

Reply With Quote
  #3 (permalink)  
Old November 7th, 2004, 07:54 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by kentown
 NO one knows the reason? or everbody here just think it's too easy?

Well, I have my doubts as to how much you can learn from someone else's half-finished, almost-but-not-quite-working homework. But here goes:

When you get user input with cin, the program waits for a user to type stuff in and press the Enter key. The contents of the input buffer include a C-language "newline" at the end of the input.

When you use cin.getline(x,30), a maximum of 29 characters are read from the buffer. If fewer than 29 characters were entered by the user, the input buffer is now empty. Then the next cin>> or getline() will wait for more user input.

When you use cin>>data.age, the numeric value is read in and the scanning of the input stops when the first non-numeric character is encountered.That means that the newline is left in the input buffer, so that the next cin>> or cin.getline() sees the newline, and thinks that's the end of that line, so it doesn't actually wait for the user to type anything new.

I suggest it might be better to use getline() to get the integer values as well as the character data, then use atoi() or something to convert the age and IQ to ints. I have seen programs that use cin.ignore() to get rid of extra characters in the input buffer.

Note that there are at least two other bugs in the program, so that even after you fix the cin stuff, it still needs some work. If you can't see what the problems are, I suggest you find a better example, or find a book with good examples to start with.

Regards,

Dave
Reply With Quote
  #4 (permalink)  
Old December 6th, 2004, 10:51 PM
Authorized User
 
Join Date: Dec 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to abelconde
Default

try this!!!

#include <string.h>
#include <iostream.h>
//you need this library to use getc() function
#include <stdio.h>

//this line goes at the top
#define MAX_STU 5

struct students {
    char name[30];
    int age;
    char grade[2];
    int iq;
};

struct students student_info(struct students data);
void print(struct students data);

void main(void) {
    //you need to put struct before students
    struct students student[MAX_STU];
    int i;

    //get the data
    //all arrays start with zero in C
    for (i=0; i<MAX_STU; ++i) {
    student[i] = student_info(student[i]);
    }

    //print the data
    //all arrays start with zero in C
    for (i=0; i<MAX_STU; ++i) {
    print(student[i]);
    }

    //this is just waiting for an input character,
    //for more information, see getc() function
    getc(stdin);
}

struct students student_info(struct students data) {
    cout << "what is the students name: ";
    //cin.getline (data.name, 30);
    //this is almost the same as the previous line
    cin >> data.name;
    cout << "What is the students age: ";
    cin >> data.age;
    cout << "What is the students grade: ";
    //cin.getline (data.grade, 2);
    //this is almost the same as the previous line
    cin >> data.grade;
    cout << "What is the students IQ: ";
    cin >> data.iq;
    //this goes to a new line
    cout << endl;
    return (data);
}


void print(struct students data) {
    cout << "Name: " << data.name << endl;
    cout << "Age: " << data.age << endl;
    cout << "Grade: " << data.grade << endl;
    cout << "IQ: " << data.iq << endl << endl;
}

Advice: next time you buy a new book, check it out in order to see if it's really good!
'Cause this example was really bad!

Regards,

------------------------
Abel Conde
abelconde@hotmail.com

Reply With Quote


Similar Threads
Thread Thread Starter Forum Replies Last Post
page jump problem netmastan Ajax 0 April 11th, 2007 04:55 PM
Please help with Jump to URL lina_d_d Crystal Reports 0 October 26th, 2006 04:59 AM
jump to combo box mjuliao Access 7 November 7th, 2005 11:19 PM
hide jump menu? lian_a Classic ASP Basics 8 December 10th, 2004 08:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.