how would I do that increment for i?
i =??
how would I do that increment for i?
i =??
You could use i = i + 1, but C programmers use the shortened ++i.
how is this?Code:#include <stdio.h> int main () { int x; int y; int i =1; scanf(" %d", &x); scanf(" %d", &y); int i= int power * x; i = i +1; while (1) { scanf("%d", &y); if(i <=y) break; return 0; }
my compiler keeps telling me that int i= int power * x is wrong..
don't use the word "int", anymore. Compiler knows that power is an int variable, already.
A few correctionsCode:#include <stdio.h> int main () { int x; int y; int i =1; scanf(" %d", &x); scanf(" %d", &y); int i= int power * x; //stop repeating the int word!! i = i +1; //goes inside your while loop, on last line, thereof while (1) { //can't use this (1) - ditch it. scanf("%d", &y); if(i <=y) //goes into your while(HERE) condition break; return 0; //make this outside the while() loop - last line of code. ++i; }
hmm it is still not working.
i've tried
i = power * x;
and int i = power * x;
any suggestions?
You can't declare int power, when it's on the right side of an equation. You can use power, but not before it's declared (created with the right data type).
so
int power;
i = power * x;
so instead of a while (1), do i just leave the while there by it self or take it out also?
Leave it, remove the "1" from inside the parenthesis. You need to add a condition for while, which your assignment specified.
Remove the break statement, and the other stuff I mentioned, and I'll be back in 1.5 hours. Dinner time!
do i still need the if statement? i was thinking can't i just take the if statement and put it in the while statement?
g. Test i to see if it is less than or equal to y in the
condition of a while statement.
Since you only need to get the value of y once, you should do that before the while loop, not inside of it. Also (it is hard to judge by the specifications you gave, but...) I would assume that the purpose of the while loop is to trap the value of i until it is greater than y (so the condition is "while less than or equal to"):
By my reading, the "program" produced by this assignment does not really do anything coherent -- the purpose may be to just get you to write some functional statements, within the context of a single program.Code:while (i<=y) i++;
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
would this be the final code?Code:#include <stdio.h> int main () { int x; int y; int i =1; int power; scanf(" %d", &x); scanf(" %d", &y); i= power * x; while (i<=y) i++; scanf("%d", &y); return 0; ++i; printf("int power is i"); }
You only need to scanf for y the first time, right? There is no mention of repeatedly inputing y until it is less than i or something?
Note that the while loop I just gave
does not have any brackets because there is only one statement in the loop. It means the same as this:Code:while (i<=y) i++;
In case you misunderstood.Code:while (i<=y) { i++; }
A difficulty here is that because there is no real purpose to the program, it will be difficult for you to assess whether everything has been done "correctly".
Are you translating the specifications from another language?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
i do not believe so. it is just part of my assignments.