Убираем ошибки в программе.
Пользователи, просматривающие топик: none
|
Зашли как: Guest
|
Имя |
Сообщение |
<< Старые топики Новые топики >> |
|
|
Убираем ошибки в программе. - 2011-04-06 21:36:26.120000
|
|
|
Alexvoin7
Сообщений: 94
Оценки: 0
Присоединился: 2010-09-04 10:35:19.600000
|
Компилирую программу, запускаю и вот такие ошибки: - Калькулятор пропускает ввод первого числа - Matrix пропускает ввод Y/N Все мои попытки исправить ошибки не к чему не приходили. Пожалуйста помогите. Заранее спасибо! Вот исходник:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
void Calculator(void);
void Game(void);
int rnd(int range);
void seedrnd(void);
void Matrix(void);
int rnd(int range);
void seedrnd(void);
int main()
{
char num;
printf("*********************************************\n");
printf("Enter number, what do you want:\n\n");
printf("1.Calculator\n");
printf("2.Game\n");
printf("Bonus:\n\n");
printf("9.Matrix\n\n\n");
num=getchar();
switch(num)
{
case '1':
Calculator();
break;
case '2':
Game();
break;
case '9':
Matrix();
break;
default:
printf("OK. Goodbye!\n");
}
getch();
return(0);
}
void Calculator(void)
{
int a;
int b;
int c;
int c1;
char A[8];
char B[8];
char C[8];
char ch;
printf("Press first number...\n");
gets(A);
a=atoi(A);
printf("Press + or - or * or /\n");
gets(C);
printf("Press second number...\n");
gets(B);
b=atoi(B);
switch(c)
{
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
}
printf("Your number %i",c);
}
void Game(void)
{
int you;
int PC;
printf("Press Enter to throw dices\n");
printf("Press Enter to START...\n");
getch();
srand(time(NULL));
you=rand() % 12 + 1;
printf("%i\n",you);
PC=rand() % 12 + 1;
printf("%i\n",PC);
if(you>PC)
{
printf("You WIN!!!\n");
}
else if(PC>you)
{
printf("You LOSE!!!\n");
}
else
{
printf("DRAW!!!\n");
}
getch();
}
void Matrix(void)
{
int i;
char A[8];
printf("Do you want to start matrix? (Y/N)\n");
HANDLE consoleOutput;
consoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consoleOutput, 0 | 0 | 10);
gets(A);
if(A[0]=='Y' || A[0]=='y')
{
for(;;)
{
printf("%d",rnd);
}
}
else
{
printf("OK! Goodbye!\n");
}
getch();
}
int rnd(int range)
{
int r;
r=rand()%range;
return(r);
}
void seedrnd(void)
{
srand((unsigned)time (NULL));
}
|
|
|
RE: Убираем ошибки в программе. - 2011-04-07 12:37:43.436666
|
|
|
_SaZ_
Сообщений: 4329
Оценки: 398
Присоединился: 2008-01-30 02:18:05.553333
|
Перед gets делай fflush(stdin), а лучше используй scanf, которая предназначена для чтения чисел с клавиатуры.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-07 17:43:13.440000
|
|
|
Alexvoin7
Сообщений: 94
Оценки: 0
Присоединился: 2010-09-04 10:35:19.600000
|
Сделал, все вводится, но в ответе всегда -1. Почему? Я пытался возвращать значение, но это не помогло.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int Calculator(void);
void Game(void);
int rnd(int range);
void seedrnd(void);
void Matrix(void);
int rnd(int range);
void seedrnd(void);
int main()
{
char num;
int i;
printf("*********************************************\n");
printf("Enter number, what do you want:\n\n");
printf("1.Calculator\n");
printf("2.Game\n");
printf("Bonus:\n\n");
printf("9.Matrix\n\n\n");
num=getchar();
switch(num)
{
case '1':
Calculator();
break;
case '2':
Game();
break;
case '9':
Matrix();
break;
default:
printf("OK. Goodbye!\n");
}
getch();
return(0);
}
int Calculator(void)
{
int a;
int b;
int c;
char A[8];
char B[8];
char C[8];
char ch;
printf("Press first number...\n");
fflush(stdin);
scanf("%d",A);
a=atoi(A);
printf("Press + or - or * or /\n");
fflush(stdin);
gets(C);
printf("Press second number...\n");
fflush(stdin);
scanf("%d",B);
b=atoi(B);
switch(c)
{
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
}
printf("Your number %d",c);
}
void Game(void)
{
int you;
int PC;
printf("Press Enter to throw dices\n");
printf("Press Enter to START...\n");
getch();
srand(time(NULL));
you=rand() % 12 + 1;
printf("%i\n",you);
PC=rand() % 12 + 1;
printf("%i\n",PC);
if(you>PC)
{
printf("You WIN!!!\n");
}
else if(PC>you)
{
printf("You LOSE!!!\n");
}
else
{
printf("DRAW!!!\n");
}
getch();
}
void Matrix(void)
{
int i;
char A[8];
printf("Do you want to start matrix? (Y/N)\n");
HANDLE consoleOutput;
consoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consoleOutput, 0 | 0 | 10);
gets(A);
if(A[0]=='Y' || A[0]=='y')
{
for(;;)
{
printf("%d",rnd);
}
}
else
{
printf("OK! Goodbye!\n");
}
getch();
}
int rnd(int range)
{
int r;
r=rand()%range;
return(r);
}
void seedrnd(void)
{
srand((unsigned)time (NULL));
}
|
|
|
RE: Убираем ошибки в программе. - 2011-04-07 19:37:48.323333
|
|
|
_SaZ_
Сообщений: 4329
Оценки: 398
Присоединился: 2008-01-30 02:18:05.553333
|
fflush(stdin); scanf("%d",A); a=atoi(A); замени на scanf( "%d", &a ); и удали все свои массивы char A[8], B[8] и т.п.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-07 19:47:20.923333
|
|
|
Alexvoin7
Сообщений: 94
Оценки: 0
Присоединился: 2010-09-04 10:35:19.600000
|
Это наверно заколдованный круг, но ошибка та же самая.:) А код такой:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int Calculator(void);
void Game(void);
int rnd(int range);
void seedrnd(void);
void Matrix(void);
int rnd(int range);
void seedrnd(void);
int main()
{
char num;
int i;
printf("*********************************************\n");
printf("Enter number, what do you want:\n\n");
printf("1.Calculator\n");
printf("2.Game\n\n");
printf("Bonus:\n\n");
printf("9.Matrix\n\n\n");
num=getchar();
switch(num)
{
case '1':
Calculator();
break;
case '2':
Game();
break;
case '9':
Matrix();
break;
default:
printf("OK. Goodbye!\n");
}
getch();
return(0);
}
int Calculator(void)
{
int a;
int b;
int c;
int d;
printf("Press first number...\n");
scanf( "%d", &a );
printf("Press + or - or * or /\n");
scanf( "%d", &d );
printf("Press second number...\n");
scanf( "%d", &b );
switch(d)
{
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
}
printf("Your number %d",c);
}
void Game(void)
{
int you;
int PC;
char i;
printf("Press Enter to throw dices\n");
printf("If you want repeat, press Enter. To Exit press ~ and Enter.\n");
printf("Press Enter to START...\n");
getch();
do
{
srand(time(NULL));
you=rand() % 12 + 1;
printf("%i\n",you);
PC=rand() % 12 + 1;
printf("%i\n",PC);
if(you>PC)
{
printf("You WIN!!!\n");
}
else if(PC>you)
{
printf("You LOSE!!!\n");
}
else if(PC==you)
{
printf("DRAW!!!\n");
}
printf("Repeat?\n");
i=getch();
}
while (i!='~');
return(0);
}
void Matrix(void)
{
int i;
char A[8];
printf("Do you want to start matrix? (Y/N)\n");
fflush(stdin);
gets(A);
HANDLE consoleOutput;
consoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consoleOutput, 0 | 0 | 10);
if(A[0]=='Y' || A[0]=='y')
{
for(;;)
{
printf("%d",rnd);
}
}
else
{
printf("OK! Goodbye!\n");
}
}
int rnd(int range)
{
int r;
r=rand()%range;
return(r);
}
void seedrnd(void)
{
srand((unsigned)time (NULL));
}
Кстати, ошибку в Matrix исправил.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-07 23:57:36.906666
|
|
|
rgo
Сообщений: 7170
Оценки: 281
Присоединился: 2004-09-25 05:14:25
|
quote:
ORIGINAL: Alexvoin7 Сделал, все вводится, но в ответе всегда -1. Почему? Потому что для чтения знака операции, ты используешь спецификатор %d. scanf ищет число, не находит его, и сообщает об ошибке. Но ты ведь не проводишь обработку ошибок ввода, поэтому в switch'е идёшь на ветку default, которая отсутствует, то есть switch ничего не делает вообще, и ты выводишь на экран в качестве результата значение неинициализированной переменной.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-08 08:19:51.386666
|
|
|
Alexvoin7
Сообщений: 94
Оценки: 0
Присоединился: 2010-09-04 10:35:19.600000
|
А как провести обработку ошибок ввода. Кстати, я в switch добавил в default и в ней прописал c=a+b; и теперь калькулятор хотя бы складывает.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-08 12:38:18.540000
|
|
|
_SaZ_
Сообщений: 4329
Оценки: 398
Присоединился: 2008-01-30 02:18:05.553333
|
Почитай описание функции scanf. Начни с секции return value.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-08 13:16:30.716666
|
|
|
BrainKill
Сообщений: 18
Оценки: 0
Присоединился: 2011-04-04 01:57:14.586666
|
1. не вижу смысла "num" декларировать как char
int num;
int i;
printf("*********************************************\n");
printf("Enter number, what do you want:\n\n");
printf("1.Calculator\n");
printf("2.Game\n\n");
printf("Bonus:\n\n");
printf("9.Matrix\n\n\n");
scanf( "%d", &num ); 2. Давно ли '+' '/' итп это int?
int Calculator(void)
{
int a;
int b;
int c;
char d;
printf("Press first number...\n");
scanf( "%d", &a );
printf("Press + or - or * or /\n");
scanf( "%c", &d );
printf("Press second number...\n");
scanf( "%d", &b );
switch(d)
{
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
}
printf("Your number %d",c);
} попробуй так, правда может где и ошибся, так как сам работаю с cout, cin.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-08 23:51:56.570000
|
|
|
Alexvoin7
Сообщений: 94
Оценки: 0
Присоединился: 2010-09-04 10:35:19.600000
|
СПАСИБО ВСЕМ!!!! Программа наконец работает в полном объеме. Вот код:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int Calculator(void);
void Game(void);
int rnd(int range);
void seedrnd(void);
void Matrix(void);
int rnd(int range);
void seedrnd(void);
int main()
{
int num;
int i;
printf("\n Welcome to MuhaNaizer 1.0\n");
printf("********************************************************************************\n");
printf("Enter number, what do you want:\n\n");
printf("1.Calculator\n");
printf("2.Game\n\n");
printf("Bonus:\n\n");
printf("9.Matrix\n\n\n");
num=getchar();
switch(num)
{
case '1':
Calculator();
break;
case '2':
Game();
break;
case '9':
Matrix();
break;
default:
printf("OK. Goodbye!\n");
}
getch();
return(0);
}
int Calculator(void)
{
int a;
int b;
int c;
char d;
printf("Press first number...\n");
scanf( "%d", &a );
printf("Press + or - or * or /\n");
fflush(stdin);
scanf( "%c", &d );
printf("Press second number...\n");
scanf( "%d", &b );
switch(d)
{
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
default:
printf("I don't understand it!\n");
}
printf("Your number %d",c);
}
void Game(void)
{
int you;
int PC;
char i;
printf("Press Enter to throw dices\n");
printf("If you want repeat, press Enter. To Exit press ~ and Enter.\n");
printf("Press Enter to START...\n");
getch();
do
{
srand(time(NULL));
you=rand() % 12 + 1;
printf("%i\n",you);
PC=rand() % 12 + 1;
printf("%i\n",PC);
if(you>PC)
{
printf("You WIN!!!\n");
}
else if(PC>you)
{
printf("You LOSE!!!\n");
}
else if(PC==you)
{
printf("DRAW!!!\n");
}
printf("Repeat?\n");
i=getch();
}
while (i!='~');
return(0);
}
void Matrix(void)
{
int i;
char A[8];
printf("Do you want to start matrix? (Y/N)\n");
fflush(stdin);
gets(A);
HANDLE consoleOutput;
consoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consoleOutput, 0 | 0 | 10);
if(A[0]=='Y' || A[0]=='y')
{
for(;;)
{
printf("%d",rnd);
}
}
else
{
printf("OK! Goodbye!\n");
}
}
int rnd(int range)
{
int r;
r=rand()%range;
return(r);
}
void seedrnd(void)
{
srand((unsigned)time (NULL));
}
Еще раз ВСЕМ СПАСИБО!!! P.S. Завтра начинаю изучать Java, а потом и С++.
|
|
|
RE: Убираем ошибки в программе. - 2011-04-09 03:02:00.843333
|
|
|
BrainKill
Сообщений: 18
Оценки: 0
Присоединился: 2011-04-04 01:57:14.586666
|
.del
|
|
|
|
|