Looked at the code, it became scary. Here is my version (written in the forehead, in fact it would be possible to make it into separate functions)
#include<stdio.h> #include<stdlib.h> #include<time.h> #include <ctype.h> int dN(int N) { srand(time(NULL)); return rand()%N + 1; } void MdN(int M, int N) { printf("==== MdN(%d,%d)===\n", M, N); // это для отладки int s = 0, tmp; if (N == 0) // защита, Ваш код падал при нуле return; while (M>0) { s += tmp = dN(N); printf("+%d", tmp); M--; } printf("=%d;\r \n", s); } int main() { int m = 0; int n = 0; while (1) { printf("Enter two numbers (MdN) or zero to exit\n"); int r = scanf("%d", &m); // ввод первого if (r != 1) { puts("first number is not numer\n"); break; } if (m == 0) { // пользователь ничего не ввел либо ввел один ноль puts("exit\n"); break; } // удалось прочитать только одно число. Попробуем прочитать ещё одно r = scanf("%d", &n); if (r != 1) { puts("ups, second number is not number\n"); break; } MdN(m,n); } printf("end\n"); return 0; }
scanf returns the result and will correctly check what it returned. In fact - returns the number of variables that were able to read. 0 - if not read anything. Those variables that could be read will have values. If something strange happens, scanf
return -1 (honestly, the constant EOF
).
You can enter as sscanf("%d %d", &m, &n)
. But if the user enters only one zero, the code will wait until one more character is entered. Therefore, I have broken the input into two parts.
Oddly enough, the code did not work correctly in both environments.
it is logical, since with 99% probability the compiler is the same - gcc.
For your case, getchar () would fit a lot with checking the entered value.
very bad advice. With int
still make check, and with float
?
Why does it work in Visual studio? there is a suspicion that there sscanf("%d %d", &m, &n)
when entering a single number and a line break returns 1 and a filled m
.
upd
Given the recent changes in the example in question. If you are just starting to learn C and make a lot of mistakes (often elementary), then the compiler needs to hint that you need to be stricter. Yes, he will quibble over trifles, but for the first time it is.
I recommend such a set
-std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes
How to use:
- if it's in the console, then just `gcc filename.st -st = c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes
- if it is monodevelop. Select your project in the "decision tree" on the left, click the right one, "properties", in the "project properties" window, find "Code generation", select a tab with the same name and see the "Extra Compiler Options" window at the bottom, where you insert the above list .
- In QtCreator you need to open the pro file and add a line like
QMAKE_CFLAGS = -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes
What do these flags mean?
std=c99
follow standard c99. Now there are others, but this one is more industrial (but this is my opinion)pedantic
be more sensitiveWall
includes a large set of checks. For example, it will stick to the code of the form if (a = 0)
(some amateurs like to treat it by turning over the conditions, and some use it for conscious entanglement). This option also includes -Wformat
, which causes the compiler to recalculate the parameters of the printf/scanf
type functions and swear if they are missing or the types do not match.Wshadow
find fault when the function uses overlapping variables. For example, inside a function, a variable is declared whose name matches the parameter. In some cases it can lead to hilarious errors.Wpointer-arith
in newer versions is already included in -pedantic
. Carp when cheating with pointer arithmetic.Wcast-qual
when casting is done, which is potentially dangerous. this often climbs with newbies when they learn to work with strings in the style of.Wstrict-prototypes
attach to int main()
, since it is desirable to write int main(int argc, char ** argv)
.Wmissing-prototypes
will require you to define prototypes of the functions used. That is, add lines of the form int dN(int N);
.
Of course, some will find these things absurd and disturbing (especially for "advanced programmers"). But I occasionally include such kits in my projects and see if nonsense has come out - it is easy to make a typo.
At first, I recommend eliminating all these compiler comments.
%d
to the appropriateprintf()
. I also advise you to end the output with the symbol '\\ n'. - avp