#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#define N 46
using namespace std;
/* Write a program that takes two arguments, two positive integers
m and n such that m < n, and prints the sum m + (m + 1) + · · · + n. */
void printUsage(){
printf("Write 2x +int: m & n, m < n; to sum them up");
}
int sum(int n, int m){
int sum1 = 0;
for ( ; m <= n; m++ ){
sum1 += m;
}
printf("%s, %d", "Suma wynosi: ", sum1);
return 0;
}
main (int argc, char **argv){
int m, n, check = 0;
if(argc != 3){
printUsage();
return -1;
}
for( int i = 1; i<argc; i++){
if (strcmp("-help", argv[i]) == 0){
printUsage;
return -1;
}
if (i+1 == argc){
printUsage();
return -1;
}
if(argv[i] > argv[i+1]){
printUsage;
cout<<"m ma byc mniejsze";
return -1;
}
check = scanf(argv[i], "%d", &m);
if(check == 0 || m<0){
printf("m ma byc liczba i wieksze od 0");
return -1;
}
assert( check == 1);
check = scanf(argv[i+1], "%d", &n);
if(check == 0 || n<0){
printf("n ma byc liczba i wieksze od 0");
return -1;
}
assert( check == 1);
assert(m<n);
}
sum( n, m );
return 0;
}
0
0
Wypadło by napisać jaki błąd wywala.
Na pewno przy printUsage
brakuje()
.
0
To ja napisałem posta, tylko nie zauważyłem, że nie jestem zalogowany ;)
- Pozwala dwa razy wpisać dane w cmd, a powinno tylko raz.
- Wyskakuje "m ma byc liczba i wieksze od 0", nawet gdy wprowadzone dane są prawidłowe.
edit: dodanie () do printUsage nic nie zmieniło