我正在进行C练习,但是我遇到问题并且我想要重复cicle(do while),事实上如果我输入1,程序再次从顶部开始,但它不会停止在gets(testo);.我尝试了很多方法来解决没有解决方案的bug,任何人都可以帮助我吗?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
int cch, cw, i, j, w, ord, f; //counter and index
char testo[80];
char alfa[50][25];
char swap[25];
do{
cch=0;
cw=0;
j=0;
w=0;
f=0;
for(i=0;i<80;i++){
testo[i]='\0';
}
printf("Write the text:\n");
gets(testo);
//initialization 2d array
for(i=0;i<50;i++){
for(j=0;j<25;j++){
alfa[i][j]='\0';
}
}
j=0;
//Count word and characters
if(testo[0]!='\0'){
cw=1;
for(i=0;testo[i]!='\0';i++){
cch++;
if(testo[i]==' '){
cw++;
j++;
}
}
}
if(cch==j){
printf("You haven't written any word\n\n");
}
else{
//Don't count double space
for(i=0;i<cch;i++){
if(testo[i]==' ' …Run Code Online (Sandbox Code Playgroud) 如果我们想要使用get in c,我们将执行以下操作:
int main(void) {
char str[100];
while (gets(str)) {
printf("%s\n",str);
}
}
Run Code Online (Sandbox Code Playgroud)
我们必须首先知道str的长度(即100),然后使用gets.是否可以在不知道c中数组长度的情况下使用gets?
我在代码中输出一些字符串时遇到问题.当我输入1个字符串然后输出它时,根本没有问题.但是,当我输入至少2个字符串时,它就失败了.字符串成为符号,除了最后一个.这是截图:

我一直在使用fflush(stdin)和fflush(stdout),但问题仍然存在.所以我该怎么做?我需要你的建议.
这是我的完整代码:
#include <stdio.h>
#include <stdlib.h>
void menu();
void entry();
void search();
void PrintSingle();
void PrintComplete();
float TotalUsed(int i);
float RegularCost(int i);
float tax(int i);
float discount(int i);
float TotalPayment(int i);
#define NMaks 101
typedef enum {false=0,true=1} boolean;
typedef struct {int BillNumber,BillClass;float LastMeter,CurrentMeter;char name[];} BillDatabase;
BillDatabase bill[NMaks];
int DataAmount=0;
int main() {
menu();
return 0;
}
void menu() {
int i;
repeat:
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[1] Entries customer information\n");
printf("[2] Search customer\n");
printf("[3] Print single bill\n"); …Run Code Online (Sandbox Code Playgroud) 我写了这个:
print "Enter your name:"
name = gets
puts "Hello #{name}. Please to meet you."
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
Hello Moemen
. Pleased to meet you
Run Code Online (Sandbox Code Playgroud)
为什么变量后面的字符串的其余部分继续在另一行?我希望它"Hello Moemen. Pleased to meet you."是我错过了什么?
我正在使用sublime text 2,我无法让gets方法让我输入数据; 它只是在控制台中输出结果,而没有让我有机会输入任何东西.任何的想法?
我做了一些研究strlen()并提出了一个问题.
假设我有一个50元素数组和一个指向第一个元素的指针,意思是:
char A[50],*x;
gets(A);
x=&A[0];
Run Code Online (Sandbox Code Playgroud)
从我的理解,strlen(x)我应该给我字符串的长度.
我的问题是,当我增加时会发生什么x?
#include<stdio.h>
int main()
{
char *str;
gets(str);
puts(str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
output =分段错误
为什么它会给出分段错误?