Why is this for loop getting ignored? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm not sure what i'm doing wrong but the for loop is not initializing
The code just goes immediately to displaying the printfs. That have no values in them since the for loop didn't activate
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("Pause")
main() {
// INITALIZE VARIABLES
int number = 0;
int i = 0;
int odd = 0;
int even = 0;
int totalNum = 0;
int tempNum = 0;
int count;
printf("Enter a number between 2 and 25\n");
scanf("%i", &number);
do{
if (number < 2 || number > 25)
printf("That was an invalid number please try again\n");
scanf("%i", &number);
} while (number < 2 || number > 25);
printf("Enter how many numbers you want to input\n");
scanf("%i", &count);
for (i = 1; i == count; ++i){
printf("input numbers\n");
scanf("%i", &tempNum);
if (tempNum % 2 == 0)
even++;
else
odd++;
totalNum = totalNum + tempNum;
} // END FOR LOOP
// DISPLAY OUTPUT
printf("You entered %i numbers\n", count);
printf("The sum of the %i numbers is %i\n", count, totalNum);
printf("The average of the %i numbers is %i\n", count, totalNum / count);
printf("You entered %i odd numbers and %i even numbers\n", odd, even);
PAUSE;
} // END MAIN

Your loop will only execute at best once, when count == 1 as you initialize i to 1.
If you enter a 1 for count,
printf("Enter how many numbers you want to input\n");
scanf("%i", &count);
the loop will run exactly once, until i increments to 2
You probably want:
for (i = 1; i <= count; ++i){

do{
if (number < 2 || number > 25)
printf("That was an invalid number please try again\n");
scanf("%i", &number);
} while (number < 2 || number > 25);
it should be...
do{
if (number < 2 || number > 25){
printf("That was an invalid number please try again\n");
scanf("%i", &number);
}
} while (number < 2 || number > 25);
else it asks always another number

i = 1, so i == count; gives false therefore the loop is ignored.

A for loop in C works like this:
for ( variable initialization; condition; variable update ) {
/* Do something... */
}
The loop will execute for as long as condition is true. So, when you do:
for (i = 1; i == count; ++i)
The loop will execute for as long as i == count is true. So, unless count holds 1 when this line is executed, the loop will never run.
As others pointed out, you probably want this:
for (i = 1; i <= count; ++i)
So your loop will run for all values of i, until it reaches count.
As a side note i should point out that the usual way to write for loops in C is something like this:
for (i = 0; i < count; i++)
We start with i = 0 because C arrays are zero-based, so the Nth element of an array has index n-1

You were so close. In addition to fixing your loop test clause for (i = 1; i <= count; i++), I would suggest using " %d" for your format specifier. Your do loop need only be a while loop to avoid printing your invalid number message every time.
Additionally, While not an error, the standard coding style for C avoids caMelCase variables in favor of all lower-case. See e.g. NASA - C Style Guide, 1994.
With those changes, (and changing your odd/even check to a simple &) you could write your code as follows.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
// #define PAUSE system("Pause")
int main (void)
{
int number, i, odd, even, totalnum, tempnum, count;
number = i = odd = even = totalnum = tempnum = count = 0;
printf ("enter a number between 2 and 25: ");
scanf (" %d", &number);
while (number < 2 || number > 25) {
printf ("invalid number, again (between 2 and 25): ");
scanf (" %d", &number);
}
printf ("numbers to input: ");
scanf (" %d", &count);
for (i = 1; i <= count; i++) {
printf ("input number %2d: ", i);
scanf (" %d", &tempnum);
if ((tempnum & 1) == 0)
even++;
else
odd++;
totalnum = totalnum + tempnum;
}
printf ("You entered %d numbers\n", count);
printf ("The sum of the %d numbers is %d\n",
count, totalnum);
printf ("The average of the %d numbers is %d\n",
count, totalnum / count);
printf ("You entered %d odd numbers and %d even numbers\n",
odd, even);
// PAUSE;
return 0;
}
note: main is type int (e.g. int main (int argc, char **argv) or simply int main (void) to indicate no arguments taken). Since it is type int it will return a value to the shell. While historic implementations may have allowed void main that is no longer the case for portable code.
Example Use/Output
$ /bin/forskipped
enter a number between 2 and 25: 4
numbers to input: 4
input number 1: 1
input number 2: 2
input number 3: 3
input number 4: 4
You entered 4 numbers
The sum of the 4 numbers is 10
The average of the 4 numbers is 2
You entered 2 odd numbers and 2 even numbers
Look it over and let me know if you have any questions.

Related

Limiting a loop in C

I have wrote a program for an assignment. The problem I am having is that I can not figure out how to make a for loop that limits the amount of data that can be entered. For example, for this assignment the user can enter lets say 100 grades. Any advice on how and where to add the for loop?
#include <stdio.h>
#include <stdlib.h>
/* Kimberly Brand - IT2240 * 05/17/2022
*/
int main (void) {
// defines a max of 100
#define maxnum 100
int i, grade[maxnum], maxentered;
i = 0;
printf ("Welcome! \n");
printf ("Please enter your grades, enter -1 when finished \n\n");
// while statemtn accepts numbers over 0
while (i < 100) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
i = 100;
} else
i++;
}
printf ("Your grades are :\n");
for (i = 0; i < maxentered; i++) {
// displays grade entered
printf ("%i", grade[i]);
printf ("\n");
}
printf ("Thank you!\n");
}
while is a looping construct, and you do already limit the amount of data that can be read.
If you initialize maxentered to maxnum (for the event where the limit is met before the user enters -1), and change the bounds of the loop as well (to avoid the magic number 100 that may not be kept in sync with the size of the array), this program will work just fine (assuming scanf never fails).
#include <stdio.h>
#include <stdlib.h>
/* Kimberly Brand - IT2240 * 05/17/2022
*/
int main (void) {
// defines a max of 100
#define maxnum 100
int i, grade[maxnum], maxentered = maxnum;
i = 0;
printf ("Welcome! \n");
printf ("Please enter your grades, enter -1 when finished \n\n");
// while statemtn accepts numbers over 0
while (i < maxnum) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
i = 100;
} else
i++;
}
printf ("Your grades are :\n");
for (i = 0; i < maxentered; i++) {
// displays grade entered
printf ("%i", grade[i]);
printf ("\n");
}
printf ("Thank you!\n");
}
That said, it can be cleaned up somewhat.
Always check the return value of scanf is the expected number of successful conversions that took place. We are looking for one conversion to occur, so we want the return value to be 1.
Once the loop is complete, the original indexing variable can be thought of as holding the length of the valid entries in the array. This can just be reused as the new bound when accessing the data.
size_t is the appropriate type for indexing memory and dealing with object sizes. While it does not matter all that much for smaller data structures, it is a good habit to practice.
#include <stdio.h>
#define MAX_ENTRIES 100
int main(void) {
int grades[MAX_ENTRIES];
size_t len = 0;
printf("Welcome!\nPlease enter your grades, enter -1 when finished.\n\n");
while (len < MAX_ENTRIES) {
printf("Please enter your grade: ");
if (1 != scanf("%d", &grades[len])) {
fprintf(stderr, "Failed to read input.\n");
return 1;
}
if (grades[len] == -1)
break;
len++;
}
printf("Your grades are:\n");
for (size_t i = 0; i < len; i++)
printf("[#%zu] %d\n", i + 1, grades[i]);
printf("Thank you!\n");
}
Instead of setting i directly to 100, you can use a break statement to exit the loop.
https://en.cppreference.com/w/cpp/language/break
// ...
while (i < 100) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
break; // <<< NEW
} else
i++;
}
// ...
Do you mean you want to change the while loop you have for a for loop?
Your while loop already limits the amount of data pretty successfully.
while (i < 100) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
i = 100;
} else
i++;
}
If you where to change (i < 100) for (i < 5) your code would limit the amount of entries in the array to 5 even though it could store 100 entries.
But a for-loop is probably better than a while loop in this scenario. It would look something like this:
for(i = 0; i < 100; i++){
//And then just add the code you want to execute inside
//I would recommend using a 'break;' statement instead of relying
//on 'i = 100' to break out of the loop.
}
As a side note, you never gave maxentered a default value, so your program will crash if you never type -1 but instead type as many grades as your loop lets you.

A question about break statement in c programming

I wrote this loop to add numbers, and the break to get out of the loop if the number entered is less than zero, and in last print the calculated numbers without adding the negative number. but the problem is even I wrote the break statement before the addition when I enter 15 and 15 and -2 the output is 28 rather than 30
I found out how to fix that, what I want to know is why
and thank you.
#include <stdio.h>
void main()
{
int j = 1, num = 0, rslt = 0;
while (1) {
if (num < 0) break;
printf("enter a number : ");
scanf("%d", &num);
rslt = rslt + num;
}
printf("the resluts are %d\n", rslt);
}
Currently, you are effectively testing the input of the previous iteration, after already adding it to your result. Instead, check the number immediately after the user enters it, before you perform any calculations.
#include <stdio.h>
int main(void)
{
int num = 0, rslt = 0;
while (1) {
printf("enter a number : ");
scanf("%d", &num);
if (num < 0)
break;
rslt += num;
}
printf("the results are %d\n", rslt);
}
You might also want to check that scanf returns the number of successful conversions you were expecting (in this case one), to handle the event where the user enters invalid input.
if (1 != scanf("%d", &num))
break;

Manipulating and tracking integers of a loop in C

so I'm fairly new to C and programming in general and also new to the website. Looking for guidance/help on figuring it all out, including help on how to properly use and ask questions on here.
So what I wanted to figure out is how to keep track of user defined numbers entered and also the value of the numbers added together. I was playing around with it for awhile looking for solutions, i was thinking there is a problem with my ways of keeping a count and not so much, there are no errors or warnings but the compiler stops after asking for an integer.
Any help or advice is much appreciated.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, sumodd, sumeven, countodd, counteven;
char name;
printf("Please enter your name. \n");
scanf("%c", &name);
printf("Please enter an integer. \n");
do {
scanf("%d", &num);
if (num%2 == 0)
sumeven = sumeven + num;
counteven = counteven + num++;
if (num%2 == 1)
sumodd = sumodd + num;
countodd = countodd + num++;
} while (num > 0);
printf("%c, the numbers you entered are broken down as follows: \n", name);
printf("You entered %d even numbers with a value totaling at %d", counteven, sumeven);
printf("You entered %d odd numbers with a value totaling at %d", countodd, sumodd);
return 0;
}
As pointed out in comments you were doing some mistakes like:
Not initializing sumodd, sumeven, countodd, counteven to zero
Not using { } in if else blocks of odd even checking
Using a character for a name variable
adding num++ to count variables instead of 1, and thereby changing its value.
I modified your code slightly :
int num, sumodd = 0, sumeven = 0, countodd = 0, counteven = 0; // initialize to zero
char name[200];
printf("Please enter your name. \n");
scanf("%s", name);
printf("Please enter an integer. \n");
do {
scanf("%d", &num);
if (num%2 == 0) {
sumeven = sumeven + num;
counteven = counteven + 1;
}
if (num%2 == 1) {
sumodd = sumodd + num;
countodd = countodd + 1;
}
} while (num > 0);
printf("%s, the numbers you entered are broken down as follows: \n", name);
printf("You entered %d even numbers with a value totaling at %d\n", counteven, sumeven);
printf("You entered %d odd numbers with a value totaling at %d\n", countodd, sumodd);
When i ran it i was able to see desired output:
~/Documents/src : $ ./a.out
Please enter your name.
Rohan
Please enter an integer.
1
2
3
0
Rohan, the numbers you entered are broken down as follows:
You entered 2 even numbers with a value totaling at 2
You entered 2 odd numbers with a value totaling at 4

Getting an Array from user? C programming

My console keeps on crashing after entering a few numbers. I am trying to get an array of 10 numbers from the user thru the console and then taking count of positives, negatives, evens, and odds. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int pos, neg, even, odd;
int nums[10];
printf("Give me 10 numbers: ");
pos = neg = even = odd = 0;
for(int i = 0; i < 10; i++){
scanf(" %d", nums[i]);
if(nums[i] > 0){
pos++;
if(nums[i] % 2 == 0){
even++;
}
else{
odd++;
}
}
else{
neg++;
}
}
printf("Positives: %d, Negatives: %d, Evens: %d, Odds: %d\n", pos, neg, even, odd);
return 0;
}
In your code,
scanf(" %d", nums[i]);
should be
scanf(" %d", &(nums[i]));
or,
scanf(" %d", nums+i);
as you need to pass the pointer to variable as the format specifier's argument in scanf() .
To elaborate, %d expects a pointer to int and what you're supplying is an int variable. it invokes undefined behavior.
That said,
Always check the return value of scanf() to ensure proper scanning.
int main() should be int main(void) to conform to the standard.
Modify scanf like scanf(" %d", &nums[i]);
scanf(" %d", nums[i]);
Scanf expects a pointer to a location to write to, and you're not giving it one.
Change your scanf to:
scanf(" %d", &(nums[i]));
to make your program work.
With this change I tested your program with stdin of
20 10 9 1 39 1 2 2 31 1
And recieved output:
Give me 10 numbers: Positives: 10, Negatives: 0, Evens: 4, Odds: 6
ideone of the thing for your testing purposes.
Change scanf(" %d", nums[i]); to scanf(" %d", &nums[i]);, because scanf() needs addresses. The parentheses around nums[i] isn't necessary, and may effect readability.
Also note that 0 is even, but not negative.
When scanf is usedto convert numbers, it expects a pointer to the corresponding type as argument, in your case int *:
scanf(" %d", &nums[i]);
This should get rid of your crash. scanf has a return value, namely the number of conversions made or the special value EOF to indicate the end of input. Please check it, otherwise you can't be sure that you have read a valid number.
When you look at your code, you'll notice that you don't need an array. Afterreading the number, you don't do aything with the array. You just keep a tally of odd, even and so on numbers. That means you just need a single integer to store the current number. That also extends your program nicely to inputs of any length.
Here's a variant that reads numbers until the end of input is reached (by pressing Ctrl-D or Ctrl-Z) or until a non-number is entered, e.g. "stop":
#include <stdlib.h>
#include <stdio.h>
int main()
{
int count = 0;
int pos = 0;
int neg = 0;
int even = 0;
int odd = 0;
int num;
while (scanf("%d", &num) == 1) {
count++;
if (num > 0) pos++;
if (num < 0) neg++;
if (num % 2 == 0) even++;
if (num % 2 != 0) odd++;
}
printf("%d numbers, of which:\n", count);
printf(" %d positive\n", pos);
printf(" %d negative\n", neg);
printf(" %d even\n", even);
printf(" %d odd\n", odd);
return 0;
}
Change scanf statement after for loop to
scanf(" %d", &nums[i]);

For loop guessing game in C

a program to get the user to guess the number that the program has picked as the lucky number. It uses one for loop and plenty of if statements. The problem is that my code stops after 2 tries, but suppose to give user 3 tries. Here is what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int iSecret, iGuess;
srand(time(NULL));
iSecret = rand() % 20 + 1;
int tries = 0;
printf("\nWelcome to the number guessing game!\nFor each game, you have at most 3 chances to guess a secret number from 1 to 20.\n");
for (tries = 0; tries < 3 || iSecret == iGuess; tries++)
{
printf("Please, enter a number between 1 and 20! ");
scanf("%d", &iGuess);
if (iGuess == iSecret)
{
printf("\nCongratulations! You won!");
return 0;
}
else if (iGuess > iSecret)
{
tries++;
printf("\nYour guess was too high.\n");
}
else if (iGuess < iSecret)
{
tries++;
printf("\nYour guess was too low.\n");
}
if (tries == 3)
break;
}
printf("\nYou have reached your third trials. The correct number is %d.\n",
iSecret);
return 0;
}
You are incrementing tries twice: once in the for definition, and also later in the body of the loop.
Remove the extra tries++ statements.
You increment tries inside the code, as well as in the for statement. Strip out the tries++ statements in the if-blocks.
You're incrementing the variable tries multiple times during loop execution,
once every turn and everytime you didn't guess your secret right
for loop already increments tries .. you don't need to do tries++ inside if statements
you don't need the || condition in for loop as you are already doing the check in if statements
here is the fixed code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int iSecret, iGuess;
srand ( time(NULL) );
iSecret = rand() % 20 + 1;
int tries = 0;
printf("\nWelcome to the number guessing game!\nFor each game, you have at most 3 chances to guess a secret number from 1 to 20.\n");
for (tries = 0; tries < 3 ; tries++) {
printf ("Please, enter a number between 1 and 20! ");
scanf ("%d", &iGuess);
if(iGuess == iSecret){
printf ("\nCongratulations! You won!");
return 0;
}
else if (iGuess > iSecret){
printf ("\nYour guess was too high.\n");
}
else if (iGuess < iSecret){
printf ("\nYour guess was too low.\n");
}
}
printf ("\nYou have reached your third trials. The correct number is %d.\n", iSecret);
return 0;
}
Output:
$ ./test
Welcome to the number guessing game!
For each game, you have at most 3 chances to guess a secret number from 1 to 20.
Please, enter a number between 1 and 20! 2
Your guess was too low.
Please, enter a number between 1 and 20! 3
Your guess was too low.
Please, enter a number between 1 and 20! 4
Your guess was too low.
You have reached your third trials. The correct number is 10.
in addition to incrementing tries too many times, the code is overly complicated, you can simplify the logic like
int main ()
{
int iSecret, iGuess;
srand ( time(NULL) );
iSecret = rand() % 20 + 1;
int tries = 0;
printf("\nWelcome to the number guessing game!\nFor each game, you have at most 3 chances to guess a secret number from 1 to 20.\n");
for (tries = 0; tries < 3; tries++) {
printf ("Please, enter a number between 1 and 20! ");
scanf ("%d", &iGuess);
if(iGuess == iSecret) {
printf ("\nCongratulations! You won!");
return 0;
}
printf ( "\nYour guess was too %s.\n", iGuess>iSecret?"high":"low");
}
printf ("\nYou have reached your third trials. The correct number is %d.\n", iSecret);
return 0;
}

Resources