我真的不知道为什么我得到错误,但后来我不太擅长这个,现在我只是想弄清楚为什么我不能打印出我的记录数组.想想任何人都能指出我正确的方向吗?它没有接近完成所以它有点粗糙......
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class student
{
private:
int id, grade;
string firstname, lastname;
public:
student();
student(int sid, string firstn, string lastn, int sgrade);
void print(student* records, int size);
};
void main()
{
string report="records.txt";
int numr=0 , sid = 0,sgrade = 0;
string firstn,lastn;
student *records=new student[7];
student stu;
student();
ifstream in;
ofstream out;
in.open(report);
in>>numr;
for(int i=0; i>7; i++)
{
in>>sid>>firstn>>lastn>>sgrade;
records[i]=student(sid, firstn,lastn,sgrade);
}
in.close();
stu.print(records, numr);
system("pause");
}
student::student()
{
} …
Run Code Online (Sandbox Code Playgroud) 我正在写一个简单的c程序,它反转一个字符串,从argv [1]中取出字符串.这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* flip_string(char *string){
int i = strlen(string);
int j = 0;
// Doesn't really matter all I wanted was the same size string for temp.
char* temp = string;
puts("This is the original string");
puts(string);
puts("This is the \"temp\" string");
puts(temp);
for(i; i>=0; i--){
temp[j] = string[i]
if (j <= strlen(string)) {
j++;
}
}
return(temp);
}
int main(int argc, char *argv[]){
puts(flip_string(argv[1]));
printf("This is the end of the program\n");
}
Run Code Online (Sandbox Code Playgroud)
基本上就是这样,程序编译并且一切都没有返回到最后的临时字符串(只是空格).在开始时,当它等于字符串时,它会打印temp.此外,如果我在for循环中使用temp的字符printf执行字符,则打印正确的临时字符串即字符串 …
好吧,请放轻松.刚学习C++,首先也在这里提问.我已经写了一个程序来列出1000以下的所有阿姆斯特朗数字.虽然我读过关于自恋数字的维基百科文章,但我只是在寻找3位数字.这意味着我只关心数字立方体的总和.
它的工作原理是执行for循环1到1000,使用用户定义的函数检查索引变量是否为非常强大,如果是,则打印它.用户定义的函数只需使用while循环来隔离数字并将多维数据集的总和与原始数字相匹配即可.如果为真,则返回1,否则返回0.
问题是,我在输出中绝对没有数字.只显示void main()中的cout语句,其余为空白.试图尽可能多地调试.编译器是Turbo C++.码-
#include<iostream.h>
#include<conio.h>
int chk_as(int);//check_armstrong
void main()
{
clrscr();
cout<<"All Armstrong numbers below 1000 are:\n";
for(int i=1;i<=1000;i++)
{
if (chk_as(i)==1)
cout<<i<<endl;
}
getch();
}
int chk_as (int n)
{
int dgt;
int sum=0,det=0;//determinant
while (n!=0)
{
dgt=n%10;
n=n/10;
sum+=(dgt*dgt*dgt);
}
if (sum==n)
{det=1;}
else
{det=0;}
return det;
}
Run Code Online (Sandbox Code Playgroud) 我是C的新手.我熟悉Python,Java,C#.因此,我遇到指针问题.
我试图使用结构在C中实现链表.截至目前,该程序创建了根成员,然后再添加一个成员以获得2个链接成员的列表.然后它调用print方法,该方法应遍历每个项目并打印它们.不幸的是,该程序陷入了无限循环,似乎是打印成员 - >数据地址而不是数据本身.
正如我之前所说,我熟悉一些OOP语言.这个问题令人沮丧,因为一个简单的链表是我应该能够在几分钟内完成的.有帮助吗?代码如下.
#include<stdio.h>
#include<stdlib.h>
struct Member{
int data;
struct Member *next;
};
struct Member *createMember(int i){
struct Member *new;
new = malloc(sizeof(struct Member));
new->data = i;
return new;
}
void print(struct Member *root){
struct Member *current = root;
while(current->next != NULL){
printf("%i, ", current->data);
current = current->next;
}
printf("%i", current->data);
}
main(){
struct Member *root;
root = createMember(15);
root->next = createMember(12);
print(root);
}
Run Code Online (Sandbox Code Playgroud) 我很难理解我的代码的运行时错误。这是我的班级作业,我认为很容易,但发生了一些奇怪的事情。我在下面发布了我的代码。
这个作业的提示是创建一个程序,要求用户输入一些单词,然后它应该输出有多少个单词。我有一个朋友运行该程序并且它有效,但是,每当我尝试运行它时,我都会收到此运行时错误。[ https://i.stack.imgur.com/Vhqbe.jpg ]
请记住,我是 C++ 和一般编程的初学者,因此我无法理解非常技术性的答复。预先感谢您的帮助。
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
int wordCounter(char *);
int main()
{
char *stringArray = nullptr;
stringArray = new char[120];
cout << "Please enter a saying or a phrase that has more than one word: " << endl;
cin.getline(stringArray, 120);
int words = wordCounter(stringArray);
cout << "Your statement contains " << words << " words." << endl;
system("pause");
return 0;
}
int wordCounter(char *stringTest)
{
char characterToTest;
int numberOfWords = …
Run Code Online (Sandbox Code Playgroud) 我试图将setw
字符串输出的设置宽度用于输出文件,但是,我无法使其工作.我跟我有以下例子.
// setw example
#include <iostream>
#include <iomanip>
#include <fstream>
int main () {
std::ofstream output_file;
output_file.open("file.txt");
output_file << "first" <<std::setw(5)<< "second"<< std::endl;
output_file.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
对于上述行我预计将有很多空间之间first
和second
,像
first second
我几乎看不到任何空格,输出就像firstsecond
我想我错过了工作setw()
注意:对于整数,它只能正常工作:
output_file << 1 <<std::setw(5)<< 2 << std::endl;
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我尝试包含几乎所有标题组合。尝试使用以下方法编译目标文件:
gcc database.c -c
gcc database.o user_interface.c -o database
Run Code Online (Sandbox Code Playgroud)
结果是:
/tmp/ccWfp2tS.o: In function `addRecord':
user_interface.c:(.text+0x4b9): multiple definition of `addRecord'
database.o:database.c:(.text+0x0): first defined here
/tmp/ccWfp2tS.o: In function `printAllRecords':
user_interface.c:(.text+0x54a): multiple definition of `printAllRecords'
database.o:database.c:(.text+0x1a): first defined here
/tmp/ccWfp2tS.o: In function `findRecord':
user_interface.c:(.text+0x590): multiple definition of `findRecord'
database.o:database.c:(.text+0x24): first defined here
/tmp/ccWfp2tS.o: In function `deleteRecord':
user_interface.c:(.text+0x5ed): multiple definition of `deleteRecord'
database.o:database.c:(.text+0x36): first defined here
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
用户界面.c
#include "record.h"
int debugmode;
int main(int argc, char *argv[])
{ …
Run Code Online (Sandbox Code Playgroud) 似乎我把PERMS所创建的文件具有相同的权限 - rwx rx rx
我尝试了755和777,权限保持不变.
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#define PERMS 0777
int main(int argc, char *argv[])
{
int createDescriptor;
char fileName[15]="Filename.txt";
if ((createDescriptor = creat(fileName, PERMS )) == -1)
{
printf("Error creating %s", fileName);
exit(EXIT_FAILURE);
}
if((close(createDescriptor))==-1)
{
write(2, "Error closing file.\n", 19);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我写了一个简单的程序.
我收到此错误:
time.cpp: In function ‘int main()’:
time.cpp:22:9: error: expected ‘;’ before ‘a’
time.cpp:23:4: error: ‘a’ was not declared in this scope
time.cpp:24:4: error: ‘b’ was not declared in this scope
time.cpp:25:4: error: ‘c’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include<iostream>
using namespace std;
class time
{
int hour;
int min;
public:
void gettime(int h,int m)
{
hour=h;
min=m;
}
void puttime()
{
cout<<hour<<endl<<min;
}
void sum(time x,time y)
{
min=x.min+y.min;
hour=min/60;
min=min%60;
hour=hour+x.hour+y.hour;
}
};
int main()
{ …
Run Code Online (Sandbox Code Playgroud) 我有2件代码:
gb_Graph = (int **)malloc(sizeof(int*)*gb_nVertices);
if (gb_Graph == NULL)
return false;
gb_Open = (VERTEX *)malloc(sizeof(VERTEX*)*gb_nVertices);
if (gb_Open == NULL)
return false;
gb_Close = (VERTEX *)malloc(sizeof(VERTEX*)*gb_nVertices);
if (gb_Close == NULL)
return false;
for (i = 0; i < gb_nVertices; i++)
{
gb_Graph[i] = (int*)malloc(sizeof(int)*gb_nVertices);
if (gb_Graph[i] == NULL)
return false;
for (j = 0; j<gb_nVertices; j++)
fscanf(gb_fInput, "%d", &(gb_Graph[i][j]));
}
for (i = 0 ; i<gb_nVertices; i++)
{
gb_Open[i].Exist = false;
gb_Open[i].ParentName = -1;
gb_Open[i].CostPath = 0;
fscanf(gb_fInput, "%d", &(gb_Open[i].CostHeuristic));
gb_Close[i].Exist …
Run Code Online (Sandbox Code Playgroud)