我有一份家庭作业,但我不明白我的代码有什么问题。名称变量搞砸了。我查看了我的教科书,但我的代码看不出任何问题。请帮忙。
名称必须以80的数组长度存储,这是我知道如何做到这一点的唯一方式。
我来自很多java经验,所以我不知道是否存在我缺少的语法或什么。
感谢您的时间。
//import statements
#include <iostream>
#include <iomanip>
using namespace std;
//class declaration section
class Student
{
//declare private instance variables
private:
int ssn;
char* name[80];
const int ARRAYLENGTH =80;
//deckale public methods and constructor
public:
Student();
void setName(string);
int getSSN();
string getName();
void setSSN(int);
};
//class implementation section
Student::Student()
{
//set ssn equal to 99999999 by default
ssn = 999999999;
//set name equal to unassigned by default
string someString ="unassigned";
for(int i = 0; i < ARRAYLENGTH; i++)
{
if (i < someString.length())
name[i] = &someString[i];
else
name[i]= "";
}
}
void Student::setName(string newName)
{
//assigns the newName to the char array
for(int i = 0; i < ARRAYLENGTH; i++)
{
if (i < newName.length())
name[i] = &newName[i];
else
name[i]= "";
}
return;
}
int Student::getSSN()
{
return ssn;
}
string Student::getName()
{
//make a string from the char array to return a string
string name1 = "";
for(int i = 0; i < 80; i++)
{
name1 += *(name+i);
}
return name1;
}
void Student::setSSN(int num)
{
//check to make sure ssn isnt below or equal to 0
if (num > 0)
ssn = num;
return;
}
//main function
int main() {
Student student1, student2;
//changing name and ssn of student2
student2.setName("John Doe");
student2.setSSN(123456789);
//printing out information
cout<< "Name for student1 is "<< student1.getName() <<" and snn is "<< student1.getSSN() <<endl; //name should be unassigned and ssn should be 999999999
cout<< "Name for student1 is "<< student2.getName() <<" and snn is "<< student2.getSSN() <<endl; //name should be John Doe and ssn should be 123456789
return 0;
}
答案 0 :(得分:4)
这个char* name[80]
声明了一个指针数组。也许您只需要使用char name[80]
来获取char数组。 (这实际上是指向数组的第一个字符的指针)
char* name[80]
是一个指针数组。 (可能不是你想要的)
答案 1 :(得分:2)
我在单次扫描中指出的一些错误是: -
这是错误的: -
name[i] = &someString[i];
您正在尝试在字符串中获取特定字符的地址,然后将其放在char *。
然后,您尝试将值存储在int范围之外: -
ssn = 999999999;
答案 2 :(得分:2)
您正在更改Student2的SSN,但您正在打印student1的SSN
答案 3 :(得分:0)
此代码应该可以解决问题。
//import statements
#include <iostream>
#include <iomanip>
using namespace std;
//class declaration section
class Student
{
//declare private instance variables
private:
int ssn;
char name[80];
const int ARRAYLENGTH =80;
//deckale public methods and constructor
public:
Student();
void setName(string);
int getSSN();
string getName();
void setSSN(int);
};
//class implementation section
Student::Student()
{
//set ssn equal to 99999999 by default
ssn = 999999999;
//set name equal to unassigned by default
string someString ="unassigned";
for(int i = 0; i < ARRAYLENGTH; i++)
{
if (i < someString.length())
name[i] = someString[i];
else
name[i]= '\0';
}
}
void Student::setName(string newName)
{
//assigns the newName to the char array
for(int i = 0; i < ARRAYLENGTH; i++)
{
if (i < newName.length())
name[i] = newName[i];
else
name[i]= '\0';
}
return;
}
int Student::getSSN()
{
return ssn;
}
string Student::getName()
{
//make a string from the char array to return a string
string name1 = "";
for(int i = 0; i < 80; i++)
{
name1 += *(name+i);
}
return name1;
}
void Student::setSSN(int num)
{
//check to make sure ssn isnt below or equal to 0
if (num > 0)
ssn = num;
return;
}
//main function
int main() {
Student student1, student2;
//changing name and ssn of student2
student2.setName("John Doe");
student2.setSSN(123456789);
//printing out information
cout<< "Name for student1 is "<< student1.getName() <<" and snn is "<< student1.getSSN() <<endl; //name should be unassigned and ssn should be 999999999
cout<< "Name for student1 is "<< student2.getName() <<" and snn is "<< student2.getSSN() <<endl; //name should be John Doe and ssn should be 123456789
return 0;
}
答案 4 :(得分:0)
我真的不认为你要存储80个名字,看起来学生只有一个名字。
char *name[80]
表示80个名字,而不是80个字符!
无论如何:你不能保持指向局部变量的指针,你在许多地方都这样做。
因此,您最好使用const char*
而不是值string
。
例如:
将string someString ="unassigned";
更改为const char* someString = "unassigned"
函数Student::setName(string newName)
应为Student::setName(const char* newName)
另一个选项是复制,而不仅仅是指定指针。表示使用string name[80];
代替char* name[80];
在这种情况下:函数Student::setName(string newName)
最好是Student::setName(const string& newName)