• ベストアンサー

setw

c++にて下記のプログラムを実行した際に、3文字までの"Hel" が表示されるかと思うのですが、実際には"Hello World"全て の文字が出力されてしまいます。 どこかおかしいのでしょうか? #include<iostream> #include<iomanip> int main(){ std::cout << std::setw(3) << "Hello World" << std::endl; return 0; } 環境は下記の通りです。 g++ : version 3.4.6 linux

質問者が選んだベストアンサー

  • ベストアンサー
  • asuncion
  • ベストアンサー率33% (2126/6288)
回答No.1

setw(3) の指定は、「少なくとも」3桁の場所が必要、 という意味で、「先頭から3桁だけ」出力する、 ということではないはずです。

flex1101
質問者

お礼

ありがとうございます。 webのプログラムサンプルで先頭から3文字だけ表示 するとありました。 文字の切り出しには使えないということですね。

関連するQ&A

  • 添字演算子

    #include <iostream> class hoge{ private: int a; public: hoge(){ a = 0; } int operator+(int fuga){ a = a + fuga; return a; } int operator[](int fuga){ return 1; } }; int main(){ hoge* p; p = new hoge; std::cout << ((*p) + 5) << std::endl; std::cout << ((*p)[1] ) << std::endl; // 5 // 1 // std::cout << ((*p) [] 1); エラーです。何故ですか? }

  • C++のiostreamのインクルードが不可能です

    C++のiostreamのインクルードが不可能です。 とても初歩的な話でお恥ずかしいのですが、 Visual Studio 2012 Express for Windows DesktopにてC++の入門をしようとしている者です。 入門書の通り、iostreamをインクルードしてnamespaceをstdにして int main() { cout << "Hello, World!" << endl; return 0; } このようなHello,World!コードを作ったのですが、エラーが発生し、 ・ソースファイルを開けません "iostream" ・識別子 "cout", "endl" が定義されていません と表示され、デバッグエラーとなります。 プロジェクトの作り手順としては C++の空のプロジェクトからcppファイルをソースファイルに追加する手順です。 どなたか解決方法をご教授ください。

  • Borland C++ Compiler 5.5

    ここで教えてもらってBorland C++ Compiler5.5とCPadをつかってるんですが、 #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; rreturn 0; } と入れると、 「hello.exeがありません」(helloは保存データの名前) と出るんです。 古いバージョン専用のスクリプトなのですか? それとも設定ミスですか? 教えてください。

  • C++でどうすればcoutで表示できるのですか?

    C++についての質問です coutで表示するときに kannsuu関数の返り値を main関数とmain関数の cout << ++kannsuu(i) << endl; の部分を変更せずに kannsuu関数のみを変更して インクリメントして「2」と表示したいのですが どのようにすればいいですか? #include <iostream> using namespace std; int kannsuu(int i) {return i;} int main() { int i = 1; cout << ++kannsuu(i) << endl; return 0; }

  • プログラミング言語Cとプログラミング言語C++

    プログラミング言語Cとプログラミング言語C++の違いって何ですか? あと、プログラミング言語C++についていくつか質問があります。 #include <iostream> using namespace std; void main(){ cout << "Hello world!" << endl; } このプログラムについて質問なんですが、iostreamってプログラミング言語Cで言うstdio.hのことですか? using namespace stdって何ですか? あと、 cout <<"Hello world!"って何ですか?

  • 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 ループに問題がありそうですが、コンパイルはちゃんとできたのに、何が悪いのでしょうか? 詳しい方、どうぞ教えてください。お願いします。

  • 6行ソースをコンパイル可能にしてください

    std::fflush(stdout); という所が間違っているんだと思いますが#include <iostream>は#include <iostream.h>にせずにコンパイルしたいです。 #include <iostream> int main(){ std::cout << "h\n"; std::fflush(stdout); return 0; }

  • プログラムの説明

    C++の初心者です。 ↓のプログラムの動作はさっぱりわかりませんが、それについての説明は具体的に教えていただきたいです。(できれば、詳しく) #include <iostream> #include <string> int getNinzu(int ARGC, char *ARGV[]) throw (char const *){ if(ARGC!=2){ throw "Needs only one argument."; } int ninzu=std::atoi(ARGV[1]); if(ninzu<=0){ throw "Value is too small."; } return ninzu; } #include <cstdlib> #include <ctime> int randfive(){ static bool firsttime=true; if(firsttime){ firsttime=false; std::srand(std::time(NULL)); } return static_cast<int>(static_cast<double>(std::rand())/RAND_MAX*(5+1)); } #include <iomanip> int main(int ARGC, char* ARGV[]){ std::string cmdname=ARGV[0]; int ninzu; try{ ninzu=getNinzu(ARGC,ARGV); std::cout << std::setfill('0'); for (int i = 1; i <= ninzu; ++i) { int score = 0; for (int k = 0; k < 20; ++k) score += randfive(); std::cout << "C" << std::setw(5) << i << " " << score << '\n'; } }catch(char const *str){ std::cerr << str << std::endl << "Usage: " << cmdname << " ninzu" << std::endl; return 1; } }

  • C++言語の非常に初歩的な質問(数表)

    今、C++言語を勉強中です。 そこで数表を作るみたいな例題があるのですがどうしても思ったとおりになってくれません。 いろんなことを考えましたが自分の力ではどうにもなりません・・・・。 そこで間違いがあれば指摘していただけたらと思い質問させていただきます。 以下がそのプログラム?です。よろしくお願いします。 #include<iostream.h> #include<math.h> #include<iomanip.h> main() { int n ; double n3 , n5; cout << setw(5) << "n" << setw(10) << "1/n" << setw(10) << "n^1/3" << "\n"; cout << setiosflags(ios::fixed); for(n=1 ; n<=25 ; ++n) { n3=1/n ; n5=pow(n,1/3) ; cout << setw(5) << n << setw(10) << setprecision(5) << n3 << setw(10) << setprecision(5) << n5 << "\n" ; } return 0 ; }

  • C++の最初のあたり

    例に自分の名前、生年月日、年齢、住所を画面に表示するプログラムを作ってみてくれませんか? そのプログラムのそれぞれの行にコメントを入れよと出てるんです まだやったばかりで文字の出力法くらいしかやってないですが… これどこが違うんですか #include<iostream> using namespace std; int main() {//main関数を定義する .... cout << "" ×○太郎 << endl ;//自分の名前 .... cout << "April 8, 19○○" << endl ;//生年月日 .... cout << "○○ years old" << endl ;//年齢 .... cout << "taitouku" << endl ;//住所を画面に表示 return 0; }//main関数の終わり そんな難しい事してないから、最初と最後の文はもうこれで行きます

専門家に質問してみよう