Keks_Stolicny 9 / 8 / 4 Регистрация: 22.10.2017 Сообщений: 162 |
||||
1 |
||||
25.12.2017, 15:26. Показов 7765. Ответов 3 Метки нет (Все метки)
В чём ошибка?
0 |
Модератор 13245 / 10387 / 6210 Регистрация: 18.12.2011 Сообщений: 27,784 |
|
25.12.2017, 15:35 |
2 |
Решение
scanf(«%i», size); Вы передаете size, а надо его адрес.
1 |
9 / 8 / 4 Регистрация: 22.10.2017 Сообщений: 162 |
|
25.12.2017, 16:06 [ТС] |
3 |
zss, спасибо, заработало, но есть новая ошибка, я не могу ввести нормально массив структур ( вводится только имя и всё Добавлено через 16 минут
0 |
Модератор 13245 / 10387 / 6210 Регистрация: 18.12.2011 Сообщений: 27,784 |
|
25.12.2017, 16:16 |
4 |
#include <iostream> И где Вы это используете?
0 |
I am writing a C++ program to search for a given integer in array, however, when I try to debug the program, visual studio ( I am using vs 2015 pro version) complains about a debug assertion failure:
enter image description here
Here is my code, its pretty straightforward:
int main() {
int searchArray[10] = { 324,4567,6789,5421345,7,65,8965,12,342,485 };
//use searchKey for the number to be found
//use location for the array index of the found value
int searchKey, location;
//write code to determine if integers entered by
//the user are in searchArray
//initiate searchKey and location
searchKey = 0;
location = 0;
int n = sizeof(searchArray) / sizeof(searchArray[0]);
//let user define the search key, give -1 to quit
while (true)
{
std::cout << "Enter an integer ('-1') to quit: ";
scanf_s("%d", searchKey);
std::cout << searchKey << "n";
if (searchKey == -1)
{
break;
}
for (location; location < n; location++)
{
if (searchArray[location] == searchKey)
{
break;
}
location = -1;
}
if (location != -1)
{
std::cout << searchKey << " is at location " << location << " in the array.n";
}
else
{
std::cout << searchKey << " is not in the array.n";
}
}
return 0;
}
Я пишу программу на C ++ для поиска заданного целого числа в массиве, однако при попытке отладки программы Visual Studio (я использую версию vs 2015 pro) жалуется на ошибку подтверждения отладки:
введите описание изображения здесь
Вот мой код, это довольно просто:
int main() {
int searchArray[10] = { 324,4567,6789,5421345,7,65,8965,12,342,485 };
//use searchKey for the number to be found
//use location for the array index of the found value
int searchKey, location;
//write code to determine if integers entered by
//the user are in searchArray
//initiate searchKey and location
searchKey = 0;
location = 0;
int n = sizeof(searchArray) / sizeof(searchArray[0]);
//let user define the search key, give -1 to quit
while (true)
{
std::cout << "Enter an integer ('-1') to quit: ";
scanf_s("%d", searchKey);
std::cout << searchKey << "n";
if (searchKey == -1)
{
break;
}
for (location; location < n; location++)
{
if (searchArray[location] == searchKey)
{
break;
}
location = -1;
}
if (location != -1)
{
std::cout << searchKey << " is at location " << location << " in the array.n";
}
else
{
std::cout << searchKey << " is not in the array.n";
}
}
return 0;
}
0
Решение
Каждый аргумент должен быть указателем на переменную типа, которая соответствует спецификатору типа в формате.
Просто измените код «scanf_s («% d «, searchKey)» на:
scanf_s("%d", &searchKey);
Это будет хорошо работать.
0
Другие решения
Других решений пока нет …
There’s some problem with the ‘calculate the total’ part but I’m unsure what it is. Everything else runs fine besides it.. I get the «result_pointer != nullptr» error everytime.
void CalculateTotal(double pricePerGallon, double* totalPtr)
//input price per gallon
//declare, ask and get the number of gallons
//calculate the total
//input/output parameter for the total
{
//declare, ask and get the number of gallons
int numGal = 0;
double tot;
printf("Enter the number of gallons requested: ");
scanf("%d", numGal);
//calculate the total
tot = numGal * pricePerGallon;
*totalPtr = tot;
printf("Your total is %f.", totalPtr);
}
unsure if it matters, but I called it in another function definition like so:
CalculateTotal(itemNumber, &total);
(I’m just learning programming for my class, so the simpler the explanation, the better. This is not C++ btw, just C.)
I’m working on this c code in visual studio 2015, and I keep getting this error message after the first scanf input. It’s something I’ve never seen before and I was hoping I could get some help with figuring out what I am doing wrong. Anything will help, thank you. I’ll post my code in the comments if it will help.