コンパイルの結果が思ったようになりません。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
rome

コンパイルの結果が思ったようになりません。

#1

投稿記事 by rome » 10年前

visual c++ 2010を使っています。
以下のコードをコンパイルして画面上に
fire と表示したいのですが、
毎回異なる文字列が画面に表示されてしまいます。

原因はクラスの値が勝手に書き換わってしまっているからでしょうか?
原因と対処法を知りたいです。よろしくお願いします。

コード:


#include "DxLib.h"
#include <string>
#include <iostream>

using namespace std;

class unitdata{
public:
//	string name;
	string type1;
//	string type2;
	int HP;
	int strengh;
	int magic;
};
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
         LPSTR lpCmdLine, int nCmdShow )
{
	unitdata lavalord;
	//lavalord.name = "lava lord";
	lavalord.type1 = "fire";
	//lavalord.type2 = "none";
	//lavalord.HP = 120;
	//lavalord.strength = 12;
    lavalord.magic = 18;

    // ウインドウモードに変更
    ChangeWindowMode( TRUE ) ;

    if( DxLib_Init() == -1 )    // DXライブラリ初期化処理
    {
         return -1;    // エラーが起きたら直ちに終了
    }
	int Color;
    // 白色の値を取得
    Color = GetColor( 255 , 255 , 255 ) ;
	int number ;

    // 文字列の描画
    //DrawFormatString( 250 , 240 - 16 , Color , lavalord.name  );
	DrawFormatString( 250 , 240 - 40 ,Color , "%s", lavalord.type1  );
	//DrawFormatString( 250 , 240 - 60 ,  Color , lavalord.type2  );
	//DrawFormatString( 250 , 240 - 80 ,  Color , lavalord.HP );
	//DrawFormatString( 250 , 240 - 100 , Color , lavalord.strength );
	//DrawFormatString( 250 , 240 - 120, Color ,lavalord.type1 );

    WaitKey() ;        // キーの入力待ち(『WaitKey』を使用)

    DxLib_End() ;        // DXライブラリ使用の終了処理

    return 0 ;        // ソフトの終了
}


アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: コンパイルの結果が思ったようになりません。

#2

投稿記事 by みけCAT » 10年前

DXライブラリのDrawFormatStringのフォーマットはprintf系の関数のそれに似ており、
%sに直接std::string型のデータを渡してもうまく動作しないことが多いと思います。
42行目を

コード:

	DrawFormatString( 250 , 240 - 40 ,Color , "%s", lavalord.type1.c_str()  );
としてみてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

rome

Re: コンパイルの結果が思ったようになりません。

#3

投稿記事 by rome » 10年前

ご指示の通りやりましたら
何とか表示できました。
ありがとうございます。

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: コンパイルの結果が思ったようになりません。

#4

投稿記事 by みけCAT » 10年前

解決でしたら、解決チェックをお願いします。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

閉鎖

“C言語何でも質問掲示板” へ戻る