Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**Use C and only <stdio.h>*** https://onlinegdb.com/HJ2aXV0P7 The above link pro

ID: 3743616 • Letter: #

Question

**Use C and only <stdio.h>***

https://onlinegdb.com/HJ2aXV0P7

The above link program involves looking at the user's input. For the program, it only takes integers, so if the user happens to input something else, like a string of characters, or character, it'll prompt the user to input an integer. The problem I'm having is that when I try to mesh these two conditions together into the while loop, then nothing happens when I enter a negative value.

Purpose of program:

This program is supposed to take positive integer input from the user, and return the integer that they have entered. However, if the user does not enter an integer or positive number, then the user is to be prompted to either input a positive number (case of a negative number) or to enter a number (case of string/characters).

Please help me, your help is greatly appreciated!

Explanation / Answer

The solution is based on simple algorithm:

there can be 3 types of input:

1. String (example : abvc200 or -abc200)

2. Negetive integer (example -200)

3. Positive integar example(200)

we need to check first if the input contains "-", then it can either be negetive number or a string with format -abc200, in that case ask the user to insert again

if above is not true, then it can be either a string or a positive number

in case of string again ask user to input, else print the positive number

--------------------------------------c code-------------------------------

#include<stdio.h>

int check(char str[])
{
//char str[50];
int i,len = 0,count = 0,j;

//printf("enter any string:: ");
//scanf("%s",str);
len = strlen(str);
//printf("%c",str[0]);
j=str[0];
if(j==45)// this for inputs which are either negetive or in format like -guwgfwugef
{
for(i=1;i<len;i++)
{
  
j=str[i];
//printf("j value %d",j);
if(j < 48 || j > 57) //48 to 57 in ASCII is for 0to 9,we will test the input by this condition
{
printf("its a string, Insert positive number ");// if its of format -uyuy then its a string
return(0);
break;
}
printf("its negetive, enter positive number "); // if its not a string its negetive, if you want to print it, put the same code that prints integer in below part
  
return 1;
}
}
  
  
j=str[0];// if above condition is not met then we will check for either positive integar or format like kshdk786hsf
if(j!=45)
{
for(i=1;i<len;i++)
{
  
j=str[i];
// printf("j value %d",j);
if(j < 48 || j > 57)   
{
printf("its a string, Insert positive number");//if its of format yfu345yfb
return(0);
break;
}
printf("its positive the number is:");
  
for(i=0;i<len;i++)// print integer
{
printf("%c",str[i]);
  
}
return(2);
}
}
  

getch();
}
int main()
{
char str[50];
int test;
while(1) //while will run unless user inputs positive
{
printf(" enter positive number:: ");
scanf("%s",str);
  
test=check(str);// send input to check function to verify
//printf("test = %d",test);
if(test==2)

break; // if check function return 2 means its positive integar then we can exit loop else it will run again
// 0 means string, 1 means negetive
}
  
}