Expression result pointer nullptr ошибка

Keks_Stolicny

9 / 8 / 4

Регистрация: 22.10.2017

Сообщений: 162

1

25.12.2017, 15:26. Показов 7765. Ответов 3

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
 
using namespace std;
 
struct pupil
{
    char name[30];
    int mark;
};
 
 
int main()
{
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    
    int size = 0;
 
    printf("Введите количество абитуриентов : ");
    scanf("%i", size);
 
    pupil *mass = new pupil [size];
 
    for (int i = 0; i < size; i++)
    {
        printf("Введите ФИО абитуриента : ");
        scanf("%s", mass[i].name);
 
        printf("Введите отметку абитуриента : ");
        scanf("%i", mass[i].mark);
 
        printf("-----------------------------------------------");
 
    }
 
    return 0;
}

В чём ошибка?



0



Модератор

Эксперт С++

13245 / 10387 / 6210

Регистрация: 18.12.2011

Сообщений: 27,784

25.12.2017, 15:35

2

Лучший ответ Сообщение было отмечено Keks_Stolicny как решение

Решение

Цитата
Сообщение от Keks_Stolicny
Посмотреть сообщение

scanf(«%i», size);

Вы передаете size, а надо его адрес.



1



9 / 8 / 4

Регистрация: 22.10.2017

Сообщений: 162

25.12.2017, 16:06

 [ТС]

3

zss, спасибо, заработало, но есть новая ошибка, я не могу ввести нормально массив структур ( вводится только имя и всё

Добавлено через 16 минут
zss, я нашёл ошибку, спасибо огромное)



0



Модератор

Эксперт С++

13245 / 10387 / 6210

Регистрация: 18.12.2011

Сообщений: 27,784

25.12.2017, 16:16

4

Цитата
Сообщение от Keks_Stolicny
Посмотреть сообщение

#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.

Понравилась статья? Поделить с друзьями:
  • Express exe ошибка при выключении
  • Explorer ошибка 403
  • Explorer ntdll dll ошибка windows 10
  • Explorer exe пустая ошибка
  • Explorer exe ошибка файловой системы 2147416359