Click here to Skip to main content
15,914,209 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to get HINSTANCE? Pin
rushing5-Feb-05 23:10
rushing5-Feb-05 23:10 
AnswerRe: How to get HINSTANCE? Pin
ThatsAlok6-Feb-05 3:03
ThatsAlok6-Feb-05 3:03 
GeneralCDC to bitmap Pin
Juanpast5-Feb-05 22:45
Juanpast5-Feb-05 22:45 
Generalexecuting Make file Pin
X20405-Feb-05 21:53
X20405-Feb-05 21:53 
GeneralRe: executing Make file Pin
ThatsAlok6-Feb-05 3:06
ThatsAlok6-Feb-05 3:06 
GeneralReturning focus to window Pin
Julietta Magakian5-Feb-05 19:59
sussJulietta Magakian5-Feb-05 19:59 
GeneralRe: Returning focus to window Pin
X20405-Feb-05 22:00
X20405-Feb-05 22:00 
GeneralBeginner need help : bug in my code Pin
Robert Eckman5-Feb-05 19:24
Robert Eckman5-Feb-05 19:24 
Confused | :confused:
I'm learning C++ and I think I have a good understanding of the basics so far. I wrote this program to take information from the console about a student, and then print it out in a simple tabular format.

The problem I'm running into is that for some particular reason, I have to getline() twice for input of the student's name, or otherwise it will be blank. For the life of me, I can't figure it out, and was wondering if someone else could help figure out why this bug is happening.

The bug occurs in function getstudentdata(), and the line is getline(cin, s.name); ... I have to have it twice, or other wise it will leave the name blank.

The error is present in both the visual c++ compiler, and the g++ linux compiler. Below is the sourcecode (which works). To see the bug in action, comment out the getline(cin, s.name); and compile/run the program. Thanks!

Sincerely,
Robert

/*
*******************************************************************************
* CSCI 202 Assignment 1b by Robert xxxxxx 03/02/2005 *
* Takes student information and displays in tab form + column for GPA *
*******************************************************************************
*/

#include <iostream>
#include <iomanip>
// #include <conio.h> // for getche() // conio.h not found on linux system

using namespace std;

/*
Design a program that takes in student information
(Name, Student-id, Year, Major, Grade in 201, Grade in 202)
and displays a nice tabular form of output with those values
AND another column for the GPA (Grade in 201 + 202 ) /2.
*/

struct studentdata // structure for holding student data
{
string name;
string id;
string major;
char g201;
char g202;
float gpa;
};


inline void clear_screen(void); // function prototype, this clears the screen
void robcenterout(string centertext); // function prototype, this centers text
void getstudentdata(studentdata& s); // getstudent() will allow me to get multiple students with an easy function;
char getgrade(void); // function prototype, this gets and validates gradeletter
float gradetofloat(char grade); // function prototype, this turns char to float
float calcgpa(char grade1, char grade2);// function prototype, this calculates GPA
void drawline(void); // function will draw line;


int main()
{
clear_screen(); // clear screen first
cout << endl << endl << endl; // add a few blank lines before showing title

robcenterout("+-------------------------------------+");
robcenterout("| Welcome to the Student Grade Report |");
robcenterout("| By Robert Eckman for CS202 |");
robcenterout("+-------------------------------------+");
robcenterout("This program will take student info ");
robcenterout("(such as name, id, major) and their ");
robcenterout("grades for CSCI 201 and CSCI 202 and ");
robcenterout("print in tabular format, inluding GPA. ");

drawline(); // draw seperator line

cout << "Please enter the number of students you wish to enter : ";
int numstudents;
cin >> numstudents; // input number of students

studentdata student[numstudents]; //create array of type studentdata

for (int i = 1; i <= numstudents; i++) // get info for students
{
clear_screen();
cout << "Please enter information about student " << i << " of " << numstudents << endl;
drawline();
getstudentdata(student[i-1]);
}

// below, we build the output for the program in tabular format

clear_screen();
drawline();
cout << setw(4) << "##";
cout << setw(25) << "Student name" << setw(9) << "ID";
cout << setw(9) << "Major" << setw(7) << "CS201";
cout << setw(7) << "CS202" << setw(7) << "GPA";
drawline();

// now we add the student data

for (int i = 1; i <= numstudents; i++)
{
cout << setw(4) << i;
cout << setw(25) << student[i-1].name << setw(9) << student[i-1].id;
cout << setw(9) << student[i-1].major << setw(7) << student[i-1].g201;
cout << setw(7) << student[i-1].g202 << setw(7) << student[i-1].gpa << endl;
}
cout << endl << endl;
} // main() is complete

inline void clear_screen(void)
{
// char a = 27u;
// cout << a << "[2J"; //clears the screen using ANSI escape sequence
system("cls"); // for windows
// system("clear"); // for linux
}

void robcenterout(string centertext)
{
int width;
width = ((80 - centertext.length())/2 + centertext.length());
cout << setw(width) << right << centertext << endl;
}

void drawline(void)
{
cout << setfill('-') << setw(78) << "\n";
cout << setfill(' ') << "\n" << left;
}

void getstudentdata(studentdata& s)
{
cout << "Please enter name of student : ";
getline(cin, s.name);
getline(cin, s.name); // for some weird reason, must have this line twice.
cout << "Please enter " << s.name << "'s student ID : ";
getline(cin, s.id);
cout << "Please enter " << s.name << "'s Major : ";
getline(cin, s.major);
cout << "Please enter grade for CSCI 201 : ";
s.g201 = getgrade();
cout << "Please enter grade for CSCI 202 : ";
s.g202 = getgrade();
s.gpa = calcgpa(s.g201,s.g202);
}

char getgrade(void)
{
bool gradeflag = false;
char grade;

while(gradeflag == false)
{
cin >> grade;
grade = toupper(grade);
// the code below will check to see if the grade is valid
if ((grade == 'A') || ( grade == 'B') || (grade == 'C') || (grade == 'D') || (grade == 'F'))
{
gradeflag = true; // if grade is a,b,c,d,f then valid flag = true
}
else
{
cout << "\nsorry, " << grade << " is incorrect, please try again : ";
}
} // end of while
return grade; // returns the char value at function's exit
}

float gradetoint(char grade)
{
switch(grade)
{
case 'A' : return 4.0; break;
case 'B' : return 3.0; break;
case 'C' : return 2.0; break;
case 'D' : return 1.0; break;
case 'F' : return 0.0; break;
}
}

float calcgpa(char grade1, char grade2)
{
float g1, g2;
g1 = gradetoint(grade1);
g2 = gradetoint(grade2);
return (g1 + g2) / 2; // calculate gpa and return float value
}
/// END OF PROGRAM


http://www2.uwsuper.edu/reckman
GeneralRe: Beginner need help : bug in my code Pin
John L. DeVito5-Feb-05 19:37
professionalJohn L. DeVito5-Feb-05 19:37 
GeneralRe: Beginner need help : bug in my code Pin
Robert Eckman5-Feb-05 19:57
Robert Eckman5-Feb-05 19:57 
GeneralRe: Beginner need help : bug in my code Pin
Robert Eckman5-Feb-05 20:16
Robert Eckman5-Feb-05 20:16 
GeneralCEdit Control to unsigned short array Pin
Dennis Wentworth5-Feb-05 11:37
sussDennis Wentworth5-Feb-05 11:37 
GeneralRe: CEdit Control to unsigned short array Pin
PJ Arends5-Feb-05 13:11
professionalPJ Arends5-Feb-05 13:11 
GeneralRe: CEdit Control to unsigned short array Pin
Dennis Wentworth5-Feb-05 13:35
sussDennis Wentworth5-Feb-05 13:35 
GeneralRe: CEdit Control to unsigned short array Pin
PJ Arends5-Feb-05 15:35
professionalPJ Arends5-Feb-05 15:35 
QuestionRedrawWindow(), Invalidate() ? Pin
BlackDice5-Feb-05 5:40
BlackDice5-Feb-05 5:40 
AnswerRe: RedrawWindow(), Invalidate() ? Pin
Mike Dimmick5-Feb-05 9:05
Mike Dimmick5-Feb-05 9:05 
GeneralRe: RedrawWindow(), Invalidate() ? Pin
BlackDice5-Feb-05 9:10
BlackDice5-Feb-05 9:10 
GeneralPlaying WAV files Pin
Grahamfff5-Feb-05 5:32
Grahamfff5-Feb-05 5:32 
GeneralRe: Playing WAV files Pin
PJ Arends5-Feb-05 10:44
professionalPJ Arends5-Feb-05 10:44 
GeneralRe: Playing WAV files Pin
Blake Miller7-Feb-05 5:25
Blake Miller7-Feb-05 5:25 
GeneralProblems with malloc Pin
r3dqu33n5-Feb-05 1:12
r3dqu33n5-Feb-05 1:12 
GeneralRe: Problems with malloc Pin
WoutL5-Feb-05 1:25
WoutL5-Feb-05 1:25 
GeneralRe: Problems with malloc Pin
r3dqu33n5-Feb-05 1:38
r3dqu33n5-Feb-05 1:38 
GeneralRe: Problems with malloc Pin
WoutL5-Feb-05 1:54
WoutL5-Feb-05 1:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.