ページ 11

includeファイルが開けません。'stdafx.h':No such file or directoryの解決方法を教えてください

Posted: 2018年4月28日(土) 10:43
by NightShift
プログラミング初心者で初投稿です。失礼がありましたら、ご容赦ください。

下記のダウンロードした、

コード:

//
// C言語の基礎的な命令のみのバージョン
//
// シャフルした山カードから、2名分3枚ずつ取る
//
// 固定配列は減らせないので、-1 はカードがないというルールにしている

#include "stdafx.h"
#include<random>

//グローバル変数
int yama[10];    // カードの山
int player[2][3];    // プレイヤー2人分で各3枚

// 順番に並べて山カードを作る
void DrawYama()
{
    for (int i = 0; i < _countof(yama); i++) //_countof(yama) は10と同じ
    {
        int card = yama[i];
        if (card >= 0)
            printf("%d ", card);    // -1はないことになっているので表示しない
    }
    printf("\n");
}

// 上から(配列の0番目から)1枚取る。無くなったら -1 が入る
int CardGet()
{
    int get = yama[0];
    for (int i = 1; i < _countof(yama); i++)
    {
        yama[i-1] = yama[i];
    }
    yama[_countof(yama) - 1] = -1;    // 無いのを -1 と規定する
    return get;
}
void YamaInit()
{
    for (int i = 0; i < _countof(yama); i++)
    {
        yama[i] = i;
    }
}
void YamaSuffle()
{
    for (int i = 0; i < _countof(yama); i++)
    {
        int r = rand() % _countof(yama);
        while (i == r)    // 同じ場所なら交換にならないのでやり直す
        {
            r = rand() % _countof(yama);
        }
        int temp = yama[i];    // 上書きされる前にtempへコピー
        yama[i] = yama[r];
        yama[r] = temp;
    }
}
void DrawPlayer(int id)
{
    for (int i = 0; i < 3; i++) //3枚表示
    {
        int card = player[id][i];
        if (card >= 0)
            printf("%d ", card);    // -1はないことになっているので表示しない
    }
    printf("\n");
}


int main()
{
    printf("0~9までのカードがある\n");
    YamaInit();
    printf("山カードの初期化\n");
    DrawYama();
    YamaSuffle();
    printf("\n\n");
    printf("シャッフルした山カード\n");
    DrawYama();
    // 2名設定、手札を山から3つ取る

    for (int id = 0; id < 2; id++)
    {
        for (int i = 0; i < 3; i++)
        {
            player[id][i] = CardGet();    // 山の上から(配列[0])1枚ずつ取る
        }
    }
    printf("payer1 : ");
    DrawPlayer(0);
    printf("payer2 : ");
    DrawPlayer(1);
    printf("残りの山カード : ");
    DrawYama();


    getchar();
    return 0;
}


/* 実行結果

0~9までのカードがある
山カードの初期化
0 1 2 3 4 5 6 7 8 9


シャッフルした山カード
3 7 0 1 2 9 8 6 4 5
payer1 : 3 7 0
payer2 : 1 2 9
残りの山カード : 8 6 4 5

*/

のソースコードをビルドしようとしたら、
includeファイルが開けません。'stdafx.h':No such file or directoryという
エラーが表示されました。解決方法を教えてください。

http://marupeke296.com/CPP_No14_PrecompiledHeader.html

https://teratail.com/questions/24721
を読んで、プリコンパイル済みのヘッダーがらみの変更を
してみましたが、上手くいきませんでした。

http://marupeke296.com/CPP_No14_PrecompiledHeader.html
に至っては
プロジェクトのプロパティにある「C/C++」->「プリコンパイル済みヘッダー」
の後に 「プリコンパイル済みヘッダーを使用する(/Yu)」に変更しても
「ファイルを使用してPCHを作成/使用」の欄 がそもそもないため、
「$(IntDir)\$(TargetName).pch」というのが自動挿入されたか
確認できませんでした。

どうか解決方法を教えて頂きますよう、お願いします。

Re: includeファイルが開けません。'stdafx.h':No such file or directoryの解決方法を教えてください

Posted: 2018年4月28日(土) 22:09
by Math
#include "stdafx.h"を使わなければ簡単なのですが(プロが作る大掛かりなプログラムでなければプリコンパイル済みのヘッダーは不要と思います。

#include "stdafx.h"を使ったプリコンパイル済みのヘッダーを使いたい場合は VS2017Community、Windows10の場合(最新版)いかのようにします。
http://www2.koyoen.birdview.co.jp/~abcxyz/x0428a.png
http://www2.koyoen.birdview.co.jp/~abcxyz/x0428b.png
http://www2.koyoen.birdview.co.jp/~abcxyz/x0428c.png
http://www2.koyoen.birdview.co.jp/~abcxyz/x0428d.png
自動作成されたものにプログラムを追加します
http://www2.koyoen.birdview.co.jp/~abcxyz/x0428e.png

Re: includeファイルが開けません。'stdafx.h':No such file or directoryの解決方法を教えてください

Posted: 2018年4月29日(日) 09:25
by NightShift
大変ありがとうございます。解決しました。