C 寻找整数
我试图让这段代码在有人输入负数甚至整数时给出一个错误。我有一个负整数,但当输入一个偶数整数时,我似乎找不到让系统给出错误消息的方法C 寻找整数,c,C,我试图让这段代码在有人输入负数甚至整数时给出一个错误。我有一个负整数,但当输入一个偶数整数时,我似乎找不到让系统给出错误消息的方法 #include <stdio.h> int main(void){ int size, i, j, spaces, stars, true, false; printf("How many rows do you want?\n"); //ask the user how many rows they want scan
#include <stdio.h>
int main(void){
int size, i, j, spaces, stars, true, false;
printf("How many rows do you want?\n"); //ask the user how many rows they want
scanf("%d", &size);
//this check to make sure a positive, odd integer is entered
if(size <= 0)
printf("sorry, but you need to input a positive, odd integer.\n");
int mid = size/2 +1; //this is to find the midpoint of the diamond where you will start decrementing
//menu options
//this is the for loop that will create your rows
for(i = 1; i <= size; i++){
if(i < mid){
spaces = mid - i;
stars = size - (spaces*2);
}
if(i > mid){
spaces = i - mid;
stars = size - (spaces*2);
}
if (i == mid){
spaces = 0;
stars = size;
}
for(j = 0; j < spaces; j++)
printf(" ");
for(j = 0; j < stars; j++)
printf("*");
printf("\n");
}
return 0;
}
#包括
内部主(空){
整数大小,i,j,空格,星星,真,假;
printf(“需要多少行?\n”);//询问用户需要多少行
scanf(“%d”,大小(&S);
//此检查用于确保输入正、奇整数
如果(大小,但如果输入了不符合这些参数的内容,程序将继续运行。我应该将其放入循环中吗?哇,谢谢,我不知道返回值。谢谢!但是如果输入了不符合这些参数的内容,程序将继续运行。我应该将其放入循环中吗?哇,谢谢,我不知道这一点返回值。谢谢!这不是您要问的问题,但您可能需要在错误检查后使用return语句…事实上,它将打印错误消息,然后继续运行。这不是您要问的问题,但您可能需要在错误检查后使用return语句…实际上,它将打印错误消息和n继续前进。
if(size % 2 == 0)
printf("sorry, even integer not allowed.\n");
//this check to make sure a positive, odd integer is entered
if(size <= 0 || size%2==0)
printf("sorry, but you need to input a positive, odd integer.\n");
//this check to make sure a positive, odd integer is entered
if(size <= 0 || size%2==0)
{
printf("sorry, but you need to input a positive, odd integer.\n");
return 1; // A return value different than 0 usually indicates an error
}