hi guys i would like u to help me in my assignment i had try to solve it but there are some of the things that making me :s i knw how to creat the name and id
string Name;
int Id;
bt wt does it mean a pointer to a dynamically allocated array of grades:s:s:S?
i jst knw how to declare a pointer like : double* Grades;
here is the assignment......
Create a Class StudentGrades with the following Data members:
Name : type String
Id: type integer
Grades: a pointer to a dynamically allocated array of Grades. Type is: pointer to double (* double)
It includes the following member functions:
A No-argument Constructor
A Constructor that takes two arguments : a String and an Integer and initializes the Name with the String and the ID with the Integer.
Set and get functions for Name, ID
A print function for Student information. It prints name, Id and the grades.
An overloaded Assignment operator of the Class Objects
A Copy Constructor of the Class Objects
Use the Syntax for the copy constructor and the overloaded assignment operator.
In another file create a C++ program that prompts the user for data to create four objects of the class StudentGrades. The first object (std1) has 5 grades, and the second (std2) has 6 grades and the third (std3) has 4 grades and fourth (std4) has no grades and data.
Then copy std2 into std4 and assign std1 to std3. Then print the details of the four objects
hey i solved my assignment bt iam trying to running it bt it doesn't work can any body please tell me where the problem is n the program
#include <iostream>
using namespace std;
class student_grades{
private:
string name,n;
int Id,i;
double* grades[];
public:
student_grades();
student_grades(sting, int);
student_grades(const student_grades&);
void set(string name,int Id){
cout << "enter the name and the ID";
cin >> n >> i;
n = name;
i = Id;
}
void get(){
return i;
return n;
}
void student_grades (student_grades&opr){
name = name.opr;
Id = Id.opr;
grades[] = grades[].opr;
}
void student_info(){
cout << "the name of the student is:" << name;
cout << "the id for the srudent is:" << Id;
grades = new double[];
cout << "the grades of the student is:" << grades[] << endl;
delete []grades;
}
};
student_grades::student_grades() {}
student_grades::student_grades(string name, int Id) {
name=" ";
Id=0;
}
student_grades::student_grades(const student_grades& copy) {
name=copy.name;
Id=copy.Id;
}
int main() {
student_grades std1;
std1.set();
cin >> std1.grades[5];
std1.get();
student_grades std2;
std2.set();
cin >> std2.grades[6];
std2.get();
student_grades std3;
std3.set();
cin >> std3.grades[4];
std3.get();
student_grades std4;
std4.set();
cin >> std4.grades[];
std4.get();
std1 = std3;
std2 = std4;
cout << std1 << std2 << std3 << std4;
return 0;
}
A dynamically allocated array is an array that isn't given a specific size until run time, unlike a fixed array declaration is at compile time. A declaration of a fixed array looks like:
int grades[500];
That array will allocate memory for 500 integers and stay that way unless you destroy it or resize it.
Creating a dynamically allocated array would look more like this:
int* grades = NULL; // A declaration in your StudentGrades class
int n = <get size from somewhere>; // Makes the size "dynamic"
grades = new int[n]; // This definition could be in your StudentGrades ctor,
// with n being a parameter.
Then you could continue by initializing all of the array elements to 0 or fill up your array with other values as needed. When you're done with the array, clean it up. This could be in your destructor:
delete [] grades;
grades = NULL;
Related
I'm practicing <thinking in c++ > for chapter5, ex01:
Write a struct called Lib that contains three string objects a, b, and c.
In main( ) create a Lib object called x and assign to x.a, x.b, and x.c.
Print out the values.
in the beginning, I'm trying:
// ex02.cpp
#include <iostream>
#include <string>
using namespace std;
struct Lib {
string a;
string b;
string c;
};
int main(){
Lib x;
x.a = 1; // here I forgot the string object, and incorrectly assign the wrong value to x.a
x.b = 2;
x.c = 3;
cout << x.a << " " << x.b << " " << x.c << endl;
return 0;
}
and it can compile successfully, but the run result seems only two blank spaces:
[root#VM-0-2-centos ch05]# g++ ex02.cpp
[root#VM-0-2-centos ch05]# ./a.out
[root#VM-0-2-centos ch05]#
at this time I find the wrong assignment. but why it should not give a compile error?
when I modify the assignment to the follows:
x.a = "hello";
x.b = "world";
x.c = "welcome";
it compiles success, and give the right run result:
[root#VM-0-2-centos ch05]# g++ ex02.cpp
[root#VM-0-2-centos ch05]# ./a.out
hello world welcome
[root#VM-0-2-centos ch05]#
my question is why x.a = 1 can compile success?
and when I try:
string test = 1;
it will compile error:
error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
U need to verify my statement by yourself. To see string code.
First, When u declare Lib x, the member of x (a, b, c) will call string constructor. So when assign value to the member of x (x.a = 1), it will call "=operation".
However, string test = 1, it will call constructor.
the most difference is caller. the type of parameter of string constructor is "const char*", but "=operation" can get other type of parameter. So, x.a = 1 at compile is passing.
Note, "int" will cast to a certain type by default.
Thx, #Muqali He, you give me the hint and direction.
I try to understand the string class from here: https://www.cplusplus.com/reference/string/string/operator=/
And I understand a bit more.
for c++98, when I use "=", there is three overload member function:
when I try:
string t2;
t2 = "test";
it's OK. and I try:
string t3;
t3 = 256;
it will get a warning:
warning: overflow in implicit constant conversion
at this time, 256 will be recognized as a char. when you output this value, it will transform into its ASCII code:
string t4;
t4 = 100;
cout << "t4= " << t4 << endl;
the ouput is
t4 = d
if I try:
string t5 = 100; // it will compile error
because there's no corresponding constructor
I'm using the boost spirit x3 semantic parser as lambda functions.
I would to like assign the value of string to an "external" variable, but my code doesn't work:
template <typename Iterator>
bool comment(Iterator first, Iterator last)
{
string varName = ""; //<- this is my "extranal" variable
auto do_varName = [&](auto& ctx)
{
cout << "VAR name: " << _attr(ctx) << endl; //This work well
//This don't work; varName is a external variable
varName = std::string( _where(ctx).begin(),
_where(ctx).end()) ;
};
The varName assumed the value of all character that are AFTER the expected content. The output of cout is correct. Why?
Instruction for program:
Read the list of names from “names.txt” in the format “First Last”.
Sort the names based upon typical alphabetic order of peoples names based upon last name then first name.
Write the sorted list to a file called “sortednames.txt” in the format “Last, First”.
Here's my code: file data was stored in fullname array but now I am stuck on how to flip the first and last name in the array??
int main()
{
const int MAXNAMES = 100;
int value = 0;
string fullname[MAXNAMES];
ifstream inFile;
inFile.open("names.txt"); //open the file to excess the rainfall data
if (inFile.fail()) // testing the file
{
cout << "Error opening file. Please check that the file currently `enter code here`exist" << endl;
exit(1);
}
cout << "File successfully open" << endl;
while(!inFile.eof())
{
while(value < 100)
{
getline(inFile,fullname[value]);
value++;
}
}
return 0;
}
To flip the name around you could do the following:
string myString;
int spacePosition;
value = 0;
while(value < 100) {
myString = fullname[value];
spacePosition = myString.find(" ");
fullname[value] = myString.substr(spacePostion) + " " + myString.substr(0, spacePostion -1);
}
I have problem converting String^ containing 8 bytes as characters (as ascii) to double.
I want to take those 8 characters and convert them binary to double.
What would you recommend to do this conversion in C++/cli?
I was trying to use Marshal::Copy, Double::TryParse, etc.
Maybe I use wrong specifications of parameters, but I really lost my last hopes.
There must be something easy to do this conversion.
Thanks.
Well, the bad news is that the System.String class uses only Unicode encoding internally.
So if you give it bytes it will map them to its internal encoding hiding the original value.
The good news is that you can play with the System.Text.Encoding class to retrieve 8bits values corresponding to the unicode characters.
Here is a sample :
#include <iostream>
using namespace System;
using namespace System::Text;
int main()
{
int n = 123456;
double d = 123.456;
std::cout << n << std::endl;
std::cout << d << std::endl;
char* n_as_bytes = (char*)&n;
char* d_as_bytes = (char*)&d;
String^ n_as_string = gcnew String(n_as_bytes, 0, sizeof(n));
String^ d_as_string = gcnew String(d_as_bytes, 0, sizeof(d));
Encoding^ ascii = Encoding::GetEncoding("iso-8859-1");
array<Byte>^ n_as_array = ascii->GetBytes(n_as_string);
array<Byte>^ d_as_array = ascii->GetBytes(d_as_string);
cli::pin_ptr<unsigned char> pin_ptr_n = &n_as_array[0];
cli::pin_ptr<unsigned char> pin_ptr_d = &d_as_array[0];
unsigned char* ptr_n = pin_ptr_n;
unsigned char* ptr_d = pin_ptr_d;
int n_out = *(int*)ptr_n;
double d_out = *(double*)ptr_d;
std::cout << n_out << std::endl;
std::cout << d_out << std::endl;
return 0;
}
This should give you :
123456
123.456
123456
123.456
Not sure it is completely safe, but trying it in your context should be a good start to ensure it is viable. :)
I have run this program before and it worked fine. Then I added the "if" statements to the "set" methods and i started seeing very large numbers when I ran the program. What can I do to fix this problem or can someone enlighten me as to why this is happening?
class GradeBook{
public:
void setStudentID(int ID){
if(10000 <= studentID && studentID <= 50000){
studentID = ID;
}
}
int getStudentID(){
return studentID;
}
void setStudentGrade(int grade){
if(0 <= studentGrade && studentGrade <= 100){
studentGrade = grade;
}
}
int getStudentGrade(){
return studentGrade;
}
void displayMessage(){
cout << "Student " << getStudentID() << " has a score of " << getStudentGrade() << endl;
}
private:
int studentGrade;
int studentID;
};
int main(){
int nameOfID;
int nameOfGrade;
GradeBook gb;
cout << "Please enter a student ID: " << endl;
cin >> nameOfID;
gb.setStudentID(nameOfID);
cout << "Please enter the student's grade: " << endl;
cin >> nameOfGrade;
gb.setStudentGrade(nameOfGrade);
getchar();
gb.displayMessage();
getchar();
}
You have your comparisons wrong you meant.
if(10000 >= studentID && studentID <= 50000)
You don't have else statements to make sure the variable is initialized, thus I'd change it to:
if(10000 >= studentID && studentID <= 50000){
studentID = ID;
}
else{
studentID = 0; //or whatever value you want to mean invalid
}
That will hopefully fix your issues.
Two guesses (I'm not that good in C++):
Your variables aren't initialized; if the if-expression does not evaluate to true the private variables will never be set to anything. The "large numbers" would just be the random value that happened to be in the memory where the variable is stored.
You are reading strings with cin and passing their pointers into the set-methods. The "large numbers" would actually be (some garbled representation probably) of the pointer-address.
Edit: Actually, forget 2; this seems to suggest that it should work. My C++ is somewhat rusty ;-)
The first thing I noticed is that your ivars are not being initialized so if the input in the setters doesn't validate then the behavior you've identified is to be expected.
You should create a default constructor and initialize the two variables to 0.
when you define GradeBook gb, the private fields (studentGrade and studentID) are not initialized. Then, gb.setStudentID tries to read studentID. studentID would be random value.