Need help please, with type casting int to char to use strcat to concatinate all grades into a single expression in the output.
I have tried but it is not outputing anything for the grades,and average.
Note: I am using Microsoft Visual Studios for this program.
/*
Description: The below code will receive input from two students
as integers, with
four grades each and then calculated to display each student's
average and first
and last name.In addition, the program will also recieve students
absences and using logical
operators to output if the student has more than or equal to 3
program and will dispaly "(Student name)
(variable string) was dropped from CIS161 due to 3 absences or the
students grade (either A,B,C,or D).
Input:
1.Each grade and name(first and last) for each student and absence
#'s entered in real time from the keyboard.
Output:
1. The average, and grades and letter grades for each student will
be outputed to the screen.
The program will Concatenate the output to provide the list of
grades, note casting may be required.
*/
#define _CRT_SECURE_NO_WARNING
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
void main()
{
int StudentGrade1a = 0; // Initializing variables for
Student1
int StudentGrade1b = 0;
int StudentGrade1c = 0;
int StudentGrade1d = 0;
int gradeAverage1 = 0; //Grade average
variable, for Student1
//const int NUM_ELEMENTS = 100;
//int gradeAmount1[NUM_ELEMENTS];
//int i;
//int b;
/*
int StudentGrade2e = 0;// Initializing variables
for Student2
int StudentGrade2f = 0;
int StudentGrade2g = 0;
int StudentGrade2h = 0;
int gradeAverage2 = 0; //Grade average
variable, for Student2
*/
const int gradeAmount = 4;
char firstName1[50]; //Initialized 20
character array
char lastName1[50];
//char firstName2[101];
//char lastName2[101];
int absences1;
//absence variables for student1 and
student2
//int absences2;
//Getting user input for first and last name of the
first student.
//for (i = 0; i < NUM_ELEMENTS; ++i) {
printf("\nPlease enter Student 1's
First name: \n");
scanf("%s", firstName1);
printf("Please enter Student 1's
Last name: \n");
scanf("%s", lastName1);
printf("\n");
//Setting values to variables as
int for grades.
printf("\nPlease enter first
Student initial grade: ");
scanf("%d",
&StudentGrade1a);
printf("\nPlease enter first
Student second grade: ");
scanf("%d",
&StudentGrade1b);
printf("\nPlease enter first
Student third grade: ");
scanf("%d",
&StudentGrade1c);
printf("\nPlease enter first
Student fourth grade: ");
scanf("%d",
&StudentGrade1d);
printf("\nPlease enter Student
1's absences:");
scanf("%d", &absences1);
printf("\n");
printf("\n");
printf("\n");
char grade1A =
(char)StudentGrade1a;
char grade1B =
(char)StudentGrade1b;
char grade1C =
(char)StudentGrade1c;
char grade1D =
(char)StudentGrade1d;
//}
/*
//Getting user input for first and last name of the
second student.
printf("Please enter Student 2's First name:
\n");
scanf("%s", firstName2);
printf("Please enter Student 2's Last name:
\n");
scanf("%s", lastName2);
printf("\n");
//Setting values as grades to variables as ints for
the second student.
printf("\nPlease enter second Student initial grade:
");
scanf("%d", &StudentGrade2e);
printf("\nPlease enter second Student second grade:
");
scanf("%d", &StudentGrade2f);
printf("\nPlease enter second Student third grade:
");
scanf("%d", &StudentGrade2g);
printf("\nPlease enter second Student fourth grade:
");
scanf("%d", &StudentGrade2h);
printf("\nPlease enter second Students
absences");
scanf("%d", &absences2);
printf("\n");
printf("\n");
*/
//getting and setting grade average for both students.
GradeAmount is the constant 4.
gradeAverage1 = (StudentGrade1a + StudentGrade1b +
StudentGrade1c + StudentGrade1d) / gradeAmount;
// gradeAverage2 = (StudentGrade2e + StudentGrade2f +
StudentGrade2g + StudentGrade2h) / gradeAmount;
//Concatenating the first and last name as whole
for student1
strcat(firstName1, " ");
strcat(firstName1, lastName1);
strcat(grade1A," , ",grade1B,
" , ",grade1C,",and ",grade1D,".");
printf("\n%s", firstName1);
printf(" grades: ");
/*
printf("%d", StudentGrade1a);
printf(" , ");
printf("%d", StudentGrade1b);
printf(" , ");
printf("%d", StudentGrade1c);
printf(",and ");
printf("%d", StudentGrade1d);
printf(".");
*/
printf(" Average is: ");
printf("%d", gradeAverage1);
printf(".");
printf("\n");
//Using logical operators to determine letter
grade, and output if the student is dropped or
//if the student should be Valedictorian of the
Century.
if (absences1 <= 2 && gradeAverage1 >= 90) {
printf(firstName1);
printf(" final grade for the CIS161
is an A.");
printf("\n");
printf("\n");
printf(firstName1);
printf(" has an A and less than 3
absences and should be recommended for Valedictorian of the
Century.");
}
else if (absences1 > 2) {
printf("\n");
printf(firstName1);
printf(" was dropped from the
CIS161 due to 3 absences.");
}
else if (absences1 <= 2 && gradeAverage1
>= 80) {
printf(firstName1);
printf(" final grade for the CIS161
is an B.");
}
else if (absences1 <= 2 && gradeAverage1
>= 70) {
printf(firstName1);
printf(firstName1, " final grade
for CIS161 is an C.");
}
else if (absences1 <= 2 && gradeAverage1
>= 60) {
printf(firstName1);
printf(" final grade for CIS161 is
an D.");
}
else {
printf(firstName1);
printf(" final grade for CIS161 is
an F.");
}
printf("\n");
/*
//Concatenating the first and last name as whole for
student1
strcat(firstName2, " ");
strcat(firstName2, lastName2);
printf("\n%s", firstName2);
printf(" grades: ");
printf("%d", StudentGrade2e);
printf(" , ");
printf("%d", StudentGrade2f);
printf(" , ");
printf("%d", StudentGrade2g);
printf(",and ");
printf("%d", StudentGrade2h);
printf(".");
printf(" Average is: ");
printf("%d", gradeAverage2);
printf(".");
printf("\n");
//Using logical operators to determine letter
grade, and output if the student is dropped or
//if the student should be Valedictorian of the
Century.
if (absences2 <= 2 && gradeAverage2
>= 90) {
printf(firstName2);
printf(" final grade for the CIS161
is an A.");
printf("\n");
printf("\n");
printf(firstName2);
printf(" has an A and less than 3
absences and should be recommended for Valedictorian of the
Century.");
}
else if (absences2 > 2) {
printf(firstName2);
printf(" was dropped from the
CIS161 due to 3 absences.");
}
else if (absences2 <= 2 && gradeAverage2
>= 80) {
printf(firstName2);
printf(" final grade for the CIS161
is an B.");
}
else if (absences2 <= 2 && gradeAverage2
>= 70) {
printf(firstName2);
printf(" final grade for CIS161 is
an C.");
}
else if (absences2 <= 2 && gradeAverage2
>= 60) {
printf(firstName2);
printf(" final grade for CIS161 is
an D.");
}
else {
printf(firstName2);
printf(" final grade for CIS161 is
an F.");
}
*/
printf("\n");
printf("\n");
system("PAUSE");
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------
// Modified C Code
#define _CRT_SECURE_NO_WARNING
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
// the below function converts integer 'x' to a string
'str'
void convertIntToStr(char *str,int grade){
int i = 0,j;
// exception case when grade == 0
if(grade == 0){
str[0] = '0';
return;
}
while(grade){
str[i] = grade%10 + '0';
grade = grade/10;
i += 1;
}
//reverse the string
char temp;
for(j=0;j<=(i-1)/2;j++){
temp = str[i-1-j];
str[i-1-j] = str[j];
str[j] = temp;
}
}
int main()
{
int StudentGrade1a = 0; // Initializing variables for
Student1
int StudentGrade1b = 0;
int StudentGrade1c = 0;
int StudentGrade1d = 0;
int gradeAverage1 = 0; //Grade average variable, for Student1
//const int NUM_ELEMENTS = 100;
//int gradeAmount1[NUM_ELEMENTS];
//int i;
//int b;
/*
int StudentGrade2e = 0;// Initializing variables for
Student2
int StudentGrade2f = 0;
int StudentGrade2g = 0;
int StudentGrade2h = 0;
int gradeAverage2 = 0; //Grade average variable, for Student2
*/
const int gradeAmount = 4;
char *firstName1; //Initialized 20 character array
char *lastName1;
firstName1 = (char*)calloc(50,sizeof(char));
lastName1 = (char*)calloc(50,sizeof(char));
//char firstName2[101];
//char lastName2[101];
int absences1; //absence variables for student1 and student2
//int absences2;
//Getting user input for first and last name of the first
student.
//for (i = 0; i < NUM_ELEMENTS; ++i) {
printf("\nPlease enter Student 1's First name: \n");
scanf("%s", firstName1);
printf("Please enter Student 1's Last name: \n");
scanf("%s", lastName1);
printf("\n");
//Setting values to variables as int for grades.
printf("\nPlease enter first Student initial grade: ");
scanf("%d", &StudentGrade1a);
printf("\nPlease enter first Student second grade: ");
scanf("%d", &StudentGrade1b);
printf("\nPlease enter first Student third grade: ");
scanf("%d", &StudentGrade1c);
printf("\nPlease enter first Student fourth grade: ");
scanf("%d", &StudentGrade1d);
printf("\nPlease enter Student 1's absences:");
scanf("%d", &absences1);
printf("\n");
printf("\n");
printf("\n");
char *grade1A = (char*)calloc(10,sizeof(char));
char *grade1B = (char*)calloc(10,sizeof(char));
char *grade1C = (char*)calloc(10,sizeof(char));
char *grade1D = (char*)calloc(10,sizeof(char));
convertIntToStr(grade1A, StudentGrade1a);
convertIntToStr(grade1B, StudentGrade1b);
convertIntToStr(grade1C, StudentGrade1c);
convertIntToStr(grade1D, StudentGrade1d);
/**
char grade1A = (char)StudentGrade1a;
char grade1B = (char)StudentGrade1b;
char grade1C = (char)StudentGrade1c;
char grade1D = (char)StudentGrade1d;
*/
//}
/*
//Getting user input for first and last name of the second
student.
printf("Please enter Student 2's First name: \n");
scanf("%s", firstName2);
printf("Please enter Student 2's Last name: \n");
scanf("%s", lastName2);
printf("\n");
//Setting values as grades to variables as ints for the second
student.
printf("\nPlease enter second Student initial grade: ");
scanf("%d", &StudentGrade2e);
printf("\nPlease enter second Student second grade: ");
scanf("%d", &StudentGrade2f);
printf("\nPlease enter second Student third grade: ");
scanf("%d", &StudentGrade2g);
printf("\nPlease enter second Student fourth grade: ");
scanf("%d", &StudentGrade2h);
printf("\nPlease enter second Students absences");
scanf("%d", &absences2);
printf("\n");
printf("\n");
*/
//getting and setting grade average for both students. GradeAmount
is the constant 4.
gradeAverage1 = (StudentGrade1a + StudentGrade1b + StudentGrade1c +
StudentGrade1d) / gradeAmount;
// gradeAverage2 = (StudentGrade2e + StudentGrade2f +
StudentGrade2g + StudentGrade2h) / gradeAmount;
//Concatenating the first and last name as whole for
student1
strcat(firstName1, " ");
strcat(firstName1, lastName1);
char *d;
d = strcat(grade1A," , ");
d = strcat(d,grade1B);
d = strcat(d," , ");
d = strcat(d, grade1C);
d = strcat(d," ,and ");
d = strcat(d,grade1D);
d = strcat(d,".");
//strcat(strcat(strcat(strcat(strcat(strcat(strcat(grade1A," ,
"),grade1B)," , "),grade1C,)",and "),grade1D),".");
// strcat(grade1A," , ",grade1B,
// " , ",grade1C,",and ",grade1D,".");
printf("\n%s", firstName1);
printf(" grades : %s\n",d);
/*
printf("%d", StudentGrade1a);
printf(" , ");
printf("%d", StudentGrade1b);
printf(" , ");
printf("%d", StudentGrade1c);
printf(",and ");
printf("%d", StudentGrade1d);
printf(".");
*/
printf("Average is: ");
printf("%d", gradeAverage1);
printf(".");
printf("\n");
//Using logical operators to determine letter grade, and output
if the student is dropped or
//if the student should be Valedictorian of the Century.
if (absences1 <= 2 && gradeAverage1 >= 90) {
printf(firstName1);
printf(" final grade for the CIS161 is an A.");
printf("\n");
printf("\n");
printf(firstName1);
printf(" has an A and less than 3 absences and should be
recommended for Valedictorian of the Century.");
}
else if (absences1 > 2) {
printf("\n");
printf(firstName1);
printf(" was dropped from the CIS161 due to 3 absences.");
}
else if (absences1 <= 2 && gradeAverage1 >= 80)
{
printf(firstName1);
printf(" final grade for the CIS161 is an B.");
}
else if (absences1 <= 2 && gradeAverage1 >= 70)
{
printf(firstName1);
printf(firstName1, " final grade for CIS161 is an C.");
}
else if (absences1 <= 2 && gradeAverage1 >= 60)
{
printf(firstName1);
printf(" final grade for CIS161 is an D.");
}
else {
printf(firstName1);
printf(" final grade for CIS161 is an F.");
}
printf("\n");
/*
//Concatenating the first and last name as whole for student1
strcat(firstName2, " ");
strcat(firstName2, lastName2);
printf("\n%s", firstName2);
printf(" grades: ");
printf("%d", StudentGrade2e);
printf(" , ");
printf("%d", StudentGrade2f);
printf(" , ");
printf("%d", StudentGrade2g);
printf(",and ");
printf("%d", StudentGrade2h);
printf(".");
printf(" Average is: ");
printf("%d", gradeAverage2);
printf(".");
printf("\n");
//Using logical operators to determine letter grade, and output
if the student is dropped or
//if the student should be Valedictorian of the Century.
if (absences2 <= 2 && gradeAverage2 >= 90) {
printf(firstName2);
printf(" final grade for the CIS161 is an A.");
printf("\n");
printf("\n");
printf(firstName2);
printf(" has an A and less than 3 absences and should be
recommended for Valedictorian of the Century.");
}
else if (absences2 > 2) {
printf(firstName2);
printf(" was dropped from the CIS161 due to 3 absences.");
}
else if (absences2 <= 2 && gradeAverage2 >= 80)
{
printf(firstName2);
printf(" final grade for the CIS161 is an B.");
}
else if (absences2 <= 2 && gradeAverage2 >= 70)
{
printf(firstName2);
printf(" final grade for CIS161 is an C.");
}
else if (absences2 <= 2 && gradeAverage2 >= 60)
{
printf(firstName2);
printf(" final grade for CIS161 is an D.");
}
else {
printf(firstName2);
printf(" final grade for CIS161 is an F.");
}
*/
printf("\n");
printf("\n");
system("PAUSE");
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------
// ScreenShot of Image

-----------------------------------------------------------------------------------------------------------------------------------
Need help please, with type casting int to char to use strcat to concatinate all grades...