View previous topic :: View next topic |
Author |
Message |
sjl002 Master Cheater Reputation: 0
Joined: 31 Aug 2013 Posts: 305
|
Posted: Fri Feb 02, 2018 10:23 am Post subject: Code block problem project! |
|
|
Hello my friends
I want to create a program in C language in the Code Block to do the bottom line:
Starting from number one, then we enter natural numbers in order. When we arrive at multiples of 5, we must enter H, if you enter a number or another, print a message.
Sorry for my bad writing.
|
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Fri Feb 02, 2018 11:08 am Post subject: |
|
|
so something like
Code: | 1
2
3
4
H
6
7
8
9
H
11
12
13
14
15
<error you must enter H for multiples of 5!
H
17
<error expected 16!
|
?? sometimes examples of expected behavior is better than trying to explain the behavior, especially if you have to translate it from one language to another.
|
|
Back to top |
|
|
sjl002 Master Cheater Reputation: 0
Joined: 31 Aug 2013 Posts: 305
|
Posted: Fri Feb 02, 2018 11:24 am Post subject: |
|
|
Right.
you can help.
Code: |
1
2
3
4
H
6
7
8
9
H
11
12
13
14
15
<error you must enter H for multiples of 5!
H
17
<error expected 16!
|
Yes.I want to write C code for this.
|
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Fri Feb 02, 2018 11:52 am Post subject: |
|
|
Then the basic scenario would probably be using scanf or gets to read the input as a string. You'd then have a variable holding the expected number (starting at 1), and check if that variable is a multiple of five (the mod/remainder operator % will be helpful there)- if it's a multiple of five, you'd check if the input was "H\n" (depending on how strict you wanted to be you could compare the first character to 'h' or 'H', use stricmp/strcasecmp, or strcmp). If it does not match, then you'd print an error
- If it's not a multiple of five then you'd try to convert the input to a number, either with sscanf or atoi (stands for ASCII To Integer), if the conversion fails or the number is not equal to the expected number then you'd print an error
In any scenario that successfully matched the expected input you'd add 1 to the variable storing the expected value. Finally you'd have all of that inside a loop so that it reads the user's input again.
so something like
Code: | char input[]
int expected = 1
while true do
scanf("%s", input)
if expected mod 5 is 0 then
if input is not 'h' then print("FAIL!") else expected++ end
else
int result = atoi(input)
if result is not expected then print("FAIL!") else expected++
end |
|
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4300
|
Posted: Fri Feb 02, 2018 11:58 am Post subject: |
|
|
This really seems like a homework assignment OP should at least attempt before asking for an answer.
Post what code you have so far and others may help fix and/or improve it.
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
|