小编sir*_*isp的帖子

创建具有指定字符数的字符串

有没有办法在初始化时用c ++创建一个说256个字符的字符串?

我的部分工作要求我"1.创建一个256个字符的字符串.重复使用你的名字."

除了使用循环之外,我不太清楚如何做到这一点,但我觉得有一种更简单的方法.

c c++ string variables

34
推荐指数
1
解决办法
5万
查看次数

为struct动态分配内存

我正在学习一个C++类并且有一个赋值,它要求我为一个struct动态分配内存.我不记得曾经在课堂上讨论这个问题,我们在上课new之前只是简单地谈过了 算子.现在我必须

"动态分配学生,然后提示用户输入学生的名字,姓氏和A号码(身份证号码)."

我的结构写得像

struct Student
{
    string firstName, lastName, aNumber;
    double GPA;
};
Run Code Online (Sandbox Code Playgroud)

我尝试Student student1 = new Student;但是这不起作用,我不确定我是如何用结构动态地做这个的.

c++ struct dynamic

9
推荐指数
3
解决办法
5万
查看次数

读取名称列表到数组中

我刚刚意识到我从来没有学会从文件中读取字符串,所以我做了一些搞乱来解决它,但我的编译器有问题.

对于我的编程类,我使用visual c ++ 2010,因为它是必需的,它没有给我太多问题,所以我没有切换到任何其他.

无论如何,我的代码和我的问题.它基本上应该从文件中读取全名并将它们存储在一个数组中.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
 const int maxsize = 100;
 string friendArray[maxsize];

 ifstream friends;
 friends.open("myFriends.dat");

 int sub = 0;

 while (friendArray[sub] <= 100)
 {
   getline(friends, friendArray[sub]);
   sub++;
 }

}
Run Code Online (Sandbox Code Playgroud)

在我的while循环中,我收到了:错误:没有运算符"<="匹配这些操作数.

我和我使用的任何其他运算符都得到了同样的东西.有帮助吗?

c++

3
推荐指数
1
解决办法
2463
查看次数

如何找到排序数组的模式?

我需要编写一个函数来查找数组的模式.我不擅长提出算法,但我希望其他人知道如何做到这一点.

我知道数组的大小和每个元素中的值,并且我将数组从最小到最大排序.

数组将传递给模式函数

mode = findMode(arrayPointer,sizePointer);

更新:

看完评论后我试过这个

int findMode(int *arrPTR, const int *sizePTR)
{
    int most_found_element = arrPTR[0];
    int most_found_element_count = 0;
    int current_element = arrPTR[0];
    int current_element_count = 0;
    int count;
    for (count = 0; count < *sizePTR; count++)
    {
        if(count == arrPTR[count])
             current_element_count++;
        else if(current_element_count > most_found_element)
        {
            most_found_element = current_element;
            most_found_element_count = current_element_count;
        }
        current_element = count;
        current_element_count=1;
    }

    return most_found_element;
}
Run Code Online (Sandbox Code Playgroud)

如果有人能把我排除在外,我仍然难以掌握这个算法.我从来没有使用过矢量,所以不要真正理解其他的例子.

c++ arrays sorting algorithm

3
推荐指数
2
解决办法
6914
查看次数

找不到内存泄漏

我正在学习C++,我的讲师让我们重温动态内存分配.在下面的代码中,我似乎有内存泄漏,因为当我运行程序时,我必须在最后.

在输出中我得到的一切都是正确的,除了Data:应该列出数组中所有数字的字段,而是给我一些-3435893乱码.

Lowest:也这样做.在程序显示所有内容后,我得到一个内存错误,即在堆缓冲区结束后正在写入内存.

我是新手,但我猜测问题是当访问arrPTR [0]时,因为我得到同样的问题Lowest:而不是Highest:.我不确定,但我很感激我能得到任何帮助.

#include <iostream>

using namespace std;

int* readArray(int&);
void sortArray(int *, const int * );
int findMode(int*, const int *);
double averageNumber(int*,const int*);
void lowestHighest(int*,const int*,int&,int&);
void printFunc(int*,const int*, int, int, double);

int main ()
{
    int size = 0;
    int *arrPTR = readArray(size);
    const int *sizePTR = &size;
    sortArray(arrPTR, sizePTR);
    int mode = findMode(arrPTR,sizePTR);
    double average = averageNumber(arrPTR, sizePTR);
    int lowest = 0, highest = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers memory-leaks

2
推荐指数
1
解决办法
319
查看次数

无法从'std :: string'转换为'char'

由于其他成员的建议,完全改变了.大多数问题解决了,仍有问题.现在不会从main中的数组中输出任何名称.不确定我是否正确地将它们从功能上传回去了.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void bubblesort(string[], const int);
int sub = 0;

int main()
{
const int maxsize = 100;
string friendArray[maxsize];

ifstream friends;
friends.open("myFriends.dat");

while (sub < maxsize)
 {
  getline(friends, friendArray[sub]);
  sub++;
 }

 bubblesort(friendArray, maxsize);


 cout<<friendArray[0]<<" "<<friendArray[1]<<" "<<friendArray[2];

 system("pause");
 return 0;
}



void bubblesort(string *array, const int size)
{
    bool swap;
    string temp;

    do
    {
        swap = false;
        for (int count = 1; count < (size - 1); count++)
        {
            if(array[count-1] >array[count])
            { …
Run Code Online (Sandbox Code Playgroud)

c++ arrays sorting

1
推荐指数
1
解决办法
3805
查看次数

如何从函数返回动态分配的指针数组?

我现在正在课堂上开始动态内存分配并对它有一个正确的理解,但不能完全正确使用它.我觉得我的指针可能也不是那么好:p

我的讲师给出了创建名为readArray的函数的指令,该函数将提示用户输入一个数字作为大小来动态创建该大小的整数数组.然后我将新数组分配给指针.然后我应该提示用户填充数组.然后我应该返回新创建的数组和大小.

我无法弄清楚如何返回数组,我想在动态分配内存时,你应该在使用后删除分配以防止泄漏.

必须将数组和大小返回到main,以便将其传递给其他函数,例如排序函数.

我非常感谢我能得到的任何帮助,因为我的思维过程一直朝着错误的方向前进.

#include <iostream>
using namespace std;

int* readArray(int&);
void sortArray(int *, const int * );

int main ()
{
   int size = 0;
   int *arrPTR = readArray(size);
   const int *sizePTR = &size;
   sortArray(arrPTR, sizePTR);

   cout<<arrPTR[1]<<arrPTR[2]<<arrPTR[3]<<arrPTR[4];

        system("pause");
        return 0;
}


int* readArray(int &size)
{
   cout<<"Enter a number for size of array.\n";
   cin>>size;
   arrPTR = new int[size];

   for(int count = 0; count < (size-1); count++)
   {    
       cout<<"Enter positive numbers to completely fill the array.\n";
       cin>>*(arrPTR+count);
   } …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers dynamic

1
推荐指数
2
解决办法
2万
查看次数

是否有解决数组中无效八进制数字的问题?

我正在尝试创建一个可以保存一天中的小时的数组,这样我就可以循环播放一个时钟.

我有:

int hourArray[24] = {12, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 01, 02,
                     03, 04, 05, 06, 07, 08, 09, 10, 11};
Run Code Online (Sandbox Code Playgroud)

我按顺序得到以下数字的错误08, 09, 08, 09.

它告诉我:

错误:八进制数字无效

我以前从来没有碰到这个,我想知道它是否有任何解决方法?

c++ arrays compiler-errors octal

1
推荐指数
1
解决办法
3058
查看次数

C++从文件中读取错误的值

我的数据文件包含以下要读入的数字

/*
111
100.00
200.00
50.00
222
200.00
300.00
100
*/
Run Code Online (Sandbox Code Playgroud)

但是当while循环读入customerNumber为100时应该是111,它也会得到其他所有错误的值.比如startsBalance如同阅读

//-9255963134931783000000000000000000000000.00 
Run Code Online (Sandbox Code Playgroud)

其他一切似乎都在读同样的价值.我只是学习文件,所以任何帮助将不胜感激.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{

int customerNumber;
double beginningBalance, purchase, payments, financeCharge, endingBalance;
ifstream inputFile;
ofstream outputFile;

inputFile.open("BeginningBalance.dat");
cout<<"Cust No | Beginning Bal | Finance Charge | Purchases | Payments | Ending Balance"<<endl;

while (inputFile >> customerNumber);
    {
     inputFile >> beginningBalance;
     inputFile >> purchase;
     inputFile >> payments;
     financeCharge = beginningBalance * .01;
     endingBalance = beginningBalance + purchase + financeCharge …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
1144
查看次数

无法初始化变量

下面是我正在编写的代码,用于了解如何在C++中使用文件.我有正确读取和写入的所有内容,但我无法让我的显示器显示正确的值,因为当我尝试初始化while循环中的Total变量时它忽略了.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    int customerNumber;
    double beginningBalance, purchase, payments, financeCharge, endingBalance;
    double beginningTotal, purchaseTotal, paymentsTotal, financeChargeTotal, endingTotal;
    ifstream inputFile;
    ofstream outputFile;

    inputFile.open("BeginningBalance.dat");
    outputFile.open("EndingBalance.dat");
    cout<<"Cust No | Beginning Bal | Finance Charge | Purchases | Payments | Ending Balance"<<endl;

    while (inputFile >> customerNumber)
    {
        outputFile <<customerNumber<<endl;
        inputFile >> beginningBalance;
        inputFile >> purchase;
        inputFile >> payments;

        financeCharge = beginningBalance * .01;
        endingBlanance= beginningBalance + purchase + financeCharge - payments;

        //***********************************************
        //This is …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
87
查看次数