• ベストアンサー

C++ 永遠に質問を繰り返す方法

clow_hの回答

  • clow_h
  • ベストアンサー率54% (17/31)
回答No.3

#2のclow_hです。 warningの理由ですが、ちょいと調べたところ yをfloat型で宣言しているの原因だと思われます。 なにやら、float型を計算するときはいったんdouble型へ変換し、再びfloat型へ戻すという作業をするらしいので、 float(32bit 符号付き浮動小数点数) < double(64bit 符号付き浮動小数点数) の図式により、いったんdouble型に変換されて戻すときにDataが失われる可能性があることをコンパイラが警告していると思われます。 実行上では問題ないでしょう。何しろ使用していないと思われるビットで警告を受けているだけなので・・・ もしかしたら、まれにこの領域を使ってしまう可能性がある。というぐらいです。 この警告が出てくるのがいやだったら、yの宣言をdouble型にしましょう。 警告が消えるはずです。 参考URL http://www.pro.or.jp/~fuji/mybooks/cdiag/cdiag.4.4.html (このページに、float、doubleのことが書いてありました) 下のURLはこのページのトップページです。

参考URL:
http://www.pro.or.jp/~fuji/mybooks/cdiag/index.html
lilno
質問者

お礼

ほんとにWarningきえました! ご丁寧なアドバイスどうもありがとうございました。 URLも細かいことはまだ理解できないのですが、参考になりました。  しかし、プログラミングは本当に少しのことで、いろいろ変わってしまうんですね

関連するQ&A

  • 実行

    こんにちは。私は昨日からC++を始めたのですがなかなかうまくいかないためこちらに質問させていただくことにしました。 #include <iostream.h> int main(void) { int x; int y; cout<<"xを入力: 10"; cin>>x; cout<<"yを入力: 2"; cin>>y; cout<<"x+yは"<<x+y<<"です。\n"; return(0); } このようなプログラムを作成したのですが実行してみたところ、 xを入力: 10 これ以降下までいきません。どうすればうまくいくのでしょうか?宜しくお願いします。

  • c++ 整数値の各桁を足し合わせるプログラム

    上記の通りの質問です #include <iostream> using namespace std; int main () { int x; int sum = 0; cout << "正の整数値の各桁を足す。\n " ; do{ cout << "正の整数値:"; cin >> x; }while (x <= 0); cout << "足すと"; while (x > 0){ cout << sum += x % 10; x /= 10; return sum; } cout << "です。\n " ; } とまでは書けましたがうまくいきません。 初心者なので簡単かもしれませんがお願いします。

  • C言語/プログラミング

    int Trianglearea(int x, y) { return x * y / 2; } int main() { int x, y, ans; cout << "底辺を入力してください。\n"; cin >> x; cout << "高さを入力してください。\n"; cin >> y; cout << "三角形の面積は" <<Trianglearea(x, y) << "です。\n"; return 0; } これで、実行したらエラー発生しました。エラーを治す方法を教えてください。

  • c++ 各桁の整数値を足すプログラム

    上記の通りの質問です #include <iostream> using namespace std; int main () { int x; int sum = 0; cout << "正の整数値の各桁を足す。\n " ; do{ cout << "正の整数値:"; cin >> x; }while (x <= 0); cout << "足すと"; while (x > 0){ cout << sum += x % 10; x /= 10; } return sum; cout << "です。\n " ; } とまでは書けましたがうまくいきません。 初心者なので簡単かもしれませんがお願いします。 また、sum += x% 10 と書いたときにオペラントと一致する演算子がないとでます。

  • C++

    C++で書いた下記のプログラムが、文字の入力が1023個までなら実行されますが、1024個を超えると実行されません。なぜだか分かる方、教えてください。 #include <iostream> #include <string> using namespace std; int main(void) { string Str; cin >> Str; cout << "here"; }

  • C++で解りません。

    C++で #include <iostream.h> int main(void) { int x; int y; cout << "xを入力してください:"; cin >> x; cout << "yを入力してください:"; cin >> y; cout << "x+yは" << x + y << "です。\n"; return 0; } ----------- で、 エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) とでます。どこがおかしいのか解りません。

  • C++の配列について

    #include <iostream> using namespace std; int main() { float w[] = {1.2,2.3,3.4,4.5,5.6}; float x[] = {4.8,2.6,1.3,9.1,8.7}; float u = 0.0; int i; for(i=0;i<5;i=i++) { u += w[i] * x[i]; } cout << "u=" << u << "です\n"; return 0; } u=105.83って出たんですが、これは何をしているプログラムなんですか

  • C++の配列についてなんですが

    #include <iostream> using namespace std; int main() { int i,goukei=0; int kokugo[5]; float heikin; for(i=0;i<5;i++) { cout << "出席番号" << i+1 << "番の国語の成績を入力してください\n"; cin >> kokugo[i]; goukei = goukei + kokugo[i]; } heikin = (float)goukei/5.0; cout << "平均点は" << heikin << "点です\n"; for(i=0;i<5;i++) { cout << "出席番号" << i+1 << "番の国語の成績は" << kokugo[i]; cout << "点で、平均との差は" << heikin-(float)kokugo[i] << "です\n"; } return 0; } これを基に 数学の点数も入力する 数学の平均も出力する 国語と数学の総合的な平均点も出力する ようにするにはどうすればいいんですか?

  • C++ の while ループ

    C++の基本学習者です。Windows 10 で Visual Studio Community 2015 を使っています。 教本に載っている、機械語のプログラムをC++でシミュレーションする、というものを作ろうと、途中まで下のようなコードを書き込み、そこまで間違いがないかを確かめるために、コンパイルして実行したら、添付の写真のようなエラーメッセージが出てきました。 // ConsoleApplication65.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cmath> #include <cstring> using namespace System; using namespace std; int main() { // variables int program = 0; int accumulator = 0; int count = 0; int instructionRegister = 0; int operationCode = 0; int operand = 0; int location[100]; cout << "*** Welcome to Simpletron ***" << endl; cout << "*** Please enter your program ***" << endl; cout << "*** one instruction at a time,***" << endl; cout << "*** after the location number and and the prompt of '?' ***" << endl; cout << "*** To stop entering instructions, ***" << endl; cout << "*** enter -9999 . ***" << endl << endl; cout << setfill('0') << internal; cout << setw(3) << count << " ? "; cin >> program; while (program != -9999) { location[count] = program; count++; } return 0; } while ループに問題がありそうですが、コンパイルはちゃんとできたのに、何が悪いのでしょうか? 詳しい方、どうぞ教えてください。お願いします。

  • 二分法のC++プログラム

    #include<iostream> #include<cmath> using namespace std; int main(){ double x1, x2, c; cout <<"x1:"; cin >> x1; cout <<"x2:"; cin >> x2; while(fabs(x2-x1) > 0.00001){ c=(x1+x2)/2; if(cos(x1/2.0)*cos(c/2)>=0) x1 = c; else x2 = c; } cout << "x=" << x1 <<endl; return 0; } cos(x/2)=0の解となるxを挟んでいないx1,x2を与えると、結果は保証できないですが、それは別にいいんですか?それはダメだとするとどこを訂正すればいいんですか