Пользователь вводит данные, и, соответственно, шаблон должен быть распечатан. Он должен встречать отрицательные числа и char в качестве входных данных. Я столкнулся с отрицательным значением в качестве ввода, но когда я пытаюсь ввести char, он переходит в бесконечный цикл. Итак, как я могу встретить значение char для типа данных int в качестве входных данных.
#include<stdio.h>
#include<conio.h>
/*
C program to print the pattern allowing user
to input the no. of lines.
*/
//Declaring method for printing pattern
void printPattern(int numberOfLines);
void main()
{
char userChoice;//User's choice to continue or exit
int numberOfLines;//User's input for number line to be printed
clrscr();
//Logic for printing the pattern
do
{
printf("Enter the number of lines you want to print \n");
scanf("%d",&numberOfLines);
//Countering issue if user enters a char insted of number
/*while()
{
printf("Enter number only \n");
scanf(" %c",&numberOfLines);
}*/
//Countering issue if user enters negative number
while(numberOfLines<=0)
{
printf("Enter positive number \n");
scanf("%d",&numberOfLines);
}
//Calling method to the start printing of method
printPattern(numberOfLines);
//Taking user's choice to continue or not
printf("Press Y to continue else any other key to exit \n");
scanf(" %c",&userChoice);
}
while(userChoice == 'y' || userChoice == 'Y');
}
/*
Method definition for printing the pattern
Argument numberOfLines: User's input for number of lines
*/
void printPattern(int numberOfLines)
{
int i,j;
for(i=0 ; i<numberOfLines ; i++) //for rows
{
for(j=0 ; j<=i ; j++) //for columns
{
printf("$");
}
printf("\n"); //for going to next row after printing one
}
}```
string
и преобразованы в целое число. - person TruthSeeker   schedule 25.12.2019argv
? @Уильям Перселл - person Wackie   schedule 26.12.2019argv
— это обычное имя для второго аргумента, передаваемого вmain
. Это список строк, переданных в командной строке. - person William Pursell   schedule 27.12.2019void main()
Есть только две допустимые подписи для функции:main()
Это:int main( void )
иint main( int argc, char *argv[] )
Обратите внимание, что они оба имеют тип возвращаемого значенияint
, НЕvoid
- person user3629249   schedule 27.12.2019