我是一个C ++新手,我试图用字符串练习指针。我所做的程序只是存储用户在命令行中键入的字符串。但是我得到了段错,不知道为什么。
这是代码:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
//This code is meant to learn how to use pointers and strings
//Ask the user for who are they in the family and save it in string array!
void print_string (string* Value, int const nSize);
int get_names(string* Family_input);
int main ( int nNumberofArgs, char* pszArgs[])
{
cout << "Tis program stores your family members\n";
cout<< "Type the names and write 0 to exit\n";
string familia_string;
string* familia = &familia_string;
int family_number;
family_number=get_names(familia);
cout << "The family members are: ";
print_string(familia, family_number);
cout << endl;
return 0;
}
int get_names(string* Family_input)
{
int i=0;
string input="";
string old_input="";
while (input!="0")
{
cout << "type " << i <<" member\n";
//cin >> *(Family_input+i);
//input=*(Family_input+i);
cin >> input;
*(Family_input + old_input.length()) = input;
old_input=input;
i++;
}
return i;
}
void print_string (string* Value, int const nSize)
{// I don't want to &psValue to be changed!
for (int i=0; i<nSize; i++)
{
cout << *(Value+i) << " ";
//&psValue++;
}
}
我不确定是不是因为我没有正确使用字符串的大小,或者我没有正确使用指针,或者我必须分配内存在使用偏移之前。
答案 0 :(得分:1)
正如@kleszcz已指出的那样,行
*(Family_input + old_input.length()) = input;
错了。您正在访问您不应该访问的内存。
最简单的解决方法是稍微更改get_names
:
int get_names(string* Family_input)
{
int i=0;
string input="";
while (input!="0")
{
cout << "type " << i <<" member\n";
cin >> input;
*Family_input += input; // Just keep on appending to the input argument.
*Family_input += "\n"; // Add a newline to separate the inputs.
i++;
}
return i;
}
同时将print_string
更改为:
void print_string (string* Value)
{
cout << *Value;
}
当然,print_string
变得如此简单,你根本不需要它。
您可以更改get_names
以使用引用参数而不是指针参数。这是一种更好的做法。
int get_names(string& Family_input)
{
int i=0;
string input="";
while (input!="0")
{
cout << "type " << i <<" member\n";
cin >> input;
Family_input += input; // Just keep on appending to the input argument.
Family_input += "\n"; // Add a newline to separate the inputs.
i++;
}
return i;
}
然后,将呼叫更改为get_names
。而不是使用
family_number=get_names(familia);
使用
family_number=get_names(familia_string);
答案 1 :(得分:0)
你得到seg错误,因为你没有为一个字符串数组分配内存。
*(Family_input + old_input.length()) = input;
这完全是胡说八道。如果你有一个数组,你只需将索引增加一个而不是字符串长度。
答案 2 :(得分:0)
如果你想在不同的字符串对象中保存不同的名字,我建议:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void print_string(string** Value, int const nSize);
void get_names(string** Family_input, int count);
int main(int nNumberofArgs, char* pszArgs[]) {
cout << "This program stores your family members\n";
int family_number;
cout << "Types the number of family members";
cin >> family_number;
/*allocate the number of string pointers that you need*/
string** familia = new string*[family_number];
cout << "Type the names\n";
get_names(familia, family_number);
cout << "The family members are: ";
print_string(familia, family_number);
cout << endl;
return 0;
}
void get_names(string** Family_input, int count) {
for(int i = 0 ; i < count; i++){
cout << "type " << i << " member\n";
/*create a string obj in the heap memory*/
string *input = new string ("");
// using that way you get only a single word
cin >> *input;
/*create a string object on the stack and put its pointer in the family_array*/
Family_input[i] = input;
}
}
void print_string(string** Value, int const nSize) {
for (int i = 0; i < nSize; i++) {
cout << *(Value[i]) << " ";
}
}