I am absolutely brand new at programming and im not sure how to explain what im doing here.
The whole purpose of this piece is to enter values and then print them out in the same order. Now I wanna quit from entering values when pressing 'q' and so I have to scanf for chars but when I assign them back to the int array the values are not the same.
Hope that makes any sense to you but in any case heres my code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5000
define flush fflush(stdin)
main() {
int input[SIZE] = {0},i = 0;
int counter = 0;
char inputs, quit;
do {
system("cls");
printf("Input number ('q' to quit and display numbers entered): ");
flush;
scanf("%c",&inputs);
flush;
if (inputs == 'q')
quit = 'q';
else {
input[i] = inputs;
counter++;
i++;
}
} while (i < SIZE && quit != 'q');
for(i = 0; i < counter; i++){
printf("%i.%i\n", i + 1, input[i]);
}
system("pause");
}
Ive been trying to do this on my own btw and also researched some information online regarding chars but couldnt find anything that would help me. Thanks a lot in advance.
You should nor be getting integer through %c neither assign char values to integers variables when that is not the intention, rather you should approach something like this
i = 0;
do {
printf("Enter a number: ");
scanf("%d", &input[i]);
i++; counter++;
printf("Do you want to continue? (y/n) : ");
scanf("%c", &inputs);
} while(inputs == 'y');
or u can get the number of integer inputs upfront and loop to get that much integers.
try instead (using your original code as much as possible):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 5000
int main()
{
int input[SIZE] = {0},i = 0;
int counter = 0;
char inputs[32];
bool quite = false;
do
{
system("cls");
printf("Input number ('q' to quit and display numbers entered): ");
// read a string from user, then convert when appropr
fgets(stdin, sizeof(inputs), inputs);
if (inputs[0] == 'q')
{
quit = true;
}
else if ( isdigit(inputs[0]) )
{
input[i] = atoi(inputs); // this will disregard any ending \n
counter++;
i++;
}
}
while (i < SIZE && !quit);
for(i = 0; i < counter; i++)
{
printf("%i.%i\n", i + 1, input[i]);
}
system("pause");
}
Another variant. This one will read in characters regardless of the use of whitespaces, since it uses getchar() rather than scanf(). I'm not sure if this is what you want. It seems as though you want integers but are reading characters. So this solution may be completely off base.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5000
int main()
{
char input[SIZE] = {0};
int i = 0;
int counter = 0;
char inputs;
printf("Input number ('q' to quit and display numbers entered): ");
while (((inputs = getchar()) != EOF) && (counter < SIZE))
{
if (inputs == 'q')
break;
input[counter] = inputs;
counter++;
}
for(i = 0; i < counter; i++)
{
printf("%c\n", input[i]);
}
system("pause");
return 0;
}
If you do really want ints, this one should work.
Notice that the atoi() function can be used to convert a C-string to an int.
The fgets() function is used to read the C-string from STDIN. However, scanf("%s", input); would also work here, as opposed to the scanf("%c", &inputs); that you used.
#include <stdio.h>
#include <stdlib.h>
#define INPUT_SIZE 1000
#define SIZE 5000
int main()
{
char input[INPUT_SIZE] = {0};
int numbers[SIZE] = {0};
int i = 0;
int counter = 0;
while ((fgets(input, sizeof(input), stdin) != NULL) && (counter < SIZE))
{
system("cls");
printf("Input number ('q' to quit and display numbers entered): ");
if (input[0] == 'q')
break;
numbers[counter] = atoi(input);
counter++;
}
for(i = 0; i < counter; i++)
{
printf("%i\n", numbers[i]);
}
system("pause");
return 0;
}
Related
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
}
}
Stdin Inputs:
10
65
100
30
95
.
.
.
Is there a way to stop the code and escape from while loop after hitting the last Input?
Amount of Inputs can be N.
edition) Is there a way to calculate the amount of Stdin Inputs? This is my major question.
using namespace std;
int main() {
int input;
int i=0;
while (1){
scanf("%d", &input);
printf("%d input:%d\n", i, input);
i++;
if (i >= 5) break; //change 5 to your desired amount of inputs
}
}
Maybe you are reading an input that has an EOF (End of file)?
In that case you should stop when receiving EOF
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int input;
int i = 0;
while (scanf("%d", &input) != EOF){
printf("%d input:%d\n", i, input);
i++;
}
printf("End\n");
return 0;
}
Otherwise you can do a program that first reads N and then iterate N time
int main(void)
{
int input;
int i = 0;
int n = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &input);
printf("%d input:%d\n", i, input);
}
return 0;
}
I'm new to programming and am trying to do this guessing game by a simple C program below. When I input letters from the word "apple", every letter (p,l,e) executes the wrong guess try again statement except for the letter 'a'. I can't seem to understand what I'm doing wrong here. Any insights is highly appreciated.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define size 10
int main() {
// initialize variables
char word[size] = "apple";
char guess;
char arrayofdashes[size] = "_____";
printf("guess a letter \n");
// input loop
for (int i = 0; i < strlen(word); i++)
{
scanf(" %c", &guess);
for (int j = 0; j< strlen(word); j++ )
{
if (guess == word[j])
{
arrayofdashes[j] = guess;
printf("%s \n", arrayofdashes);
}
else
{
printf("wrong guess. Try again \n");
break;
}
}
}
}
Remove the break and add a flag variable to check the correctness of the input letter. And you need a better way to check if the word spelling is complete.
char flag;
int count = 0;
// input loop
while (count < strlen(word))
{
scanf(" %c", &guess);
flag = 0;
for (int j = 0; j< strlen(word); j++ )
{
if (guess == word[j] && guess != arrayofdashes[j])
{
arrayofdashes[j] = guess;
count++;
flag = 1;
}
}
if (flag)
printf("%s \n", arrayofdashes);
else
printf("wrong guess. Try again \n");
}
the problem is that you're using break - this drops out of your inner for-loop after comparing your input against the first character, and prevents it from being compared with subsequent characters.
What strategies have you tried for debugging this yourself? You'll have a few more changes to make aside from removing break, but figuring them out is part of the fun
for (int j = 0; j < strlen(word); j++)//no two loop
{
scanf(" %c", &guess);
if (guess == word[j])
{
arrayofdashes[j] = guess;
printf("%s \n", arrayofdashes);
}
else
{
printf("wrong guess. Try again \n");
j--;
}
}
You don't need the input loop. And if the answer is not correct you should subtract one from j.
This is what I theorized it should be but it seemed like it doesn't work.
HELP PLEAZEE
int main()
{
char input[50];
int i;
do{
printf("ENTER A CHARACTER:");
scanf("%s",&input);
if(isalpha(input)!=0){
printf("YOU INPUTTED A CHARACTER");
i++;
}else{
printf("INVALID INPUT\n");
}
}while(i!=1);
}
isalpha takes an integer as an argument.
You are giving a char array.
You should loop for the number of characters given in input[], if you want more than one character (hard to tell from this code).
This exits if you give exactly one character but keeps going if you give more than one:
#include <stdio.h>
#include <string.h>
int main()
{
char input[50];
int i = 0, j;
size_t len = 0;
do
{
printf("ENTER A CHARACTER:");
scanf("%s",&input);
len = strlen(input);
for(j = 0; j < len; j++) {
if(isalpha(input[j]))
{
i++;
printf("YOU INPUTTED A CHARACTER %d\n", i);
}
else
{
printf("INVALID INPUT\n");
break;
}
}
} while(i!=1);
}
I want to make a program that counts the vowels in a sentence entered by the user.
For that compare a character that capitalizes with the vowel arrangement, but although they do not pulls error, do not type the correct output.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
//Program EJER004
int main(){
char vowels[5] = {'A','E','I','O','U'};
int count;
char letter;
count = 0;
printf("Enter a phrase, ending with a point\n");
do{
letter=getchar();
if (toupper(letter) == vowels[5]) /*attempt ask if is a vowel the letter introduced*/
count++;
}while (letter != '.');
printf("\n");
printf("\n");
printf("The number of vowels in the phrase introduced is% d", count);
getch();
return 0;
}
It think the problem is the comparison toupper(letter) == vowels[5]? vowels[5] is always out of array, but other vowels aren't checked.
You would need to add a loop like:
char upr=toupper(letter);
for(int i=0; i<5; i++)
if(vowels[i]==c)
{ cont++;
break;
}
You should write a small function for this comparison
int checkforVowels(char tobechecked)
{
//this only get vowels in CAPSLOCK tho... so dont forget toupper
char vowels[5] = {'A','E','I','O','U'};
int hasvowel = 0;
for(int i = 0; i < 5; i++)
{
if(tobechecked == vowels[i])
{
hasvowel = 1;
break;
}
}
return hasvowel;
}
so you can have it like that
if(checkforVowels(toupper(letter))
HTH
I am trying to make a program that will use getchar() to read three integers and store it in an array but something is wrong in my code.
#include <stdio.h>
int main( )
{
printf("Please enter three digit number 100 to 999: ");
int numEntered[2];
numEntered = getchar();
for (int i = 0; i<3; i++) {
printf("%d", numEntered[i])
}
}
Try this:
#include <stdio.h>
int main(){
printf("Please enter three digit number 100 to 999: ");
int numEntered[3];
for (int i = 0; i<3; i++){
scanf("%d", &numEntered[i]);
printf("%d", numEntered[i]);
}
return 0;
}
you need to read a value inside the for loop! Second thing, by reading with getchar(), you are getting the ascii value of the character, so.. if you read "1" and print with %d, you actually printing 49!
See the ascii table here: http://www.asciitable.com/index/asciifull.gif
let's try and and think about the problem here:
do you want to read and store an integer value? if yes -> use scanf
do you want to read a number digit by digit? if yes -> use getchar
do you want to make sure what you read has exactly 3 digits? if yes...what do you do when it does not?
if reading digit by digit, make sure you are reading numbers; getchar reads characters -> use atoi funtion or check ascii value;
Putting it all together(some assumptions were made):
int main()
{
char digits[3]; // don't use ints to store chars...
printf("enter the 3 digit number - 100 to 999: ");
for (int i=0;i<3;i++) // only the first 3 chars are read
{
char c = getchar();
if (char < '0') || (char > '9')
{
printf("invalid digit!");
exit(0);
}
digits[i] = c;
}
printf("the number entered is: %c%c%c", digits[0],digits[1],digits[2]);
}
You don't use getchar to get integer values.
Use scanf() instead.
Try this:
#include <stdio.h>
int main( )
{
printf("Please enter three digit number 100 to 999: ");
int numEntered[3];
for (int i = 0; i<3; i++){
scanf("%d",&a[i]);
printf("%d", numEntered[i]);
}
}
numEntered = getchar();
(1) It can not be assigned to the array itself.
(2) getchar() reads one character.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void){
char digits[4] = {0};//4: 3 character + NUL character
int ch, value;
do {
printf("Please enter three digit number 100 to 999: ");
for(int i = 0; i < 3 && (ch = getchar()) != '\n' && ch != EOF; ++i){
digits[i] = ch;
}
if(ch != '\n')
while((ch = getchar()) != '\n')
if(!isspace(ch))//invalid input
*digits = 0;
value = atoi(digits);
} while(value < 100 || 999 < value);
char *rotate_left(char digits[4]);
printf("%d\n", value);
printf("%d\n", atoi(rotate_left(digits)));
printf("%d\n", atoi(rotate_left(digits)));
return 0;
}
char *rotate_left(char digits[4]){
char temp = digits[0];
digits[0] = digits[1];
digits[1] = digits[2];
digits[2] = temp;
return digits;
}