HE
HEL
HELL
HELLO
use loop inside loop. inner loop for iterating letters and spaces. external - for iterating rows
no. this code works. but look at this from another side. what if you need to do the same with another word e.g. "world"? or what if word is 100500 letters long?
Please send me a better solution
maybe a FOR loop ?
no. this chat is for explaining, but not for giving final solutions. just try to write next: 1. your word is an array of symbols 2. loop iterating from 0 to word length minus 1 (which means you iterating rows) 3. loop inside previous from 0 to previous loop index 4. inside this loop print symbol 5. if you need to print spaces before symbols to draw triangle - just calculate the count length/2-inner index
include <iostream> using namespace std; int main() { for (string i = H; i < HELLO; i++) { if (i == HELLO) { break; } cout << i << "\n"; } return 0; }
cout <<insert coin and try again ;
i++ is not defined for std::string
Language C++ Input include <iostream> using namespace std; int main() { for (string i = H; i < HELLO; i++) { if (i == HELLO) { break; } cout << i << "\n"; } return 0; } Output /usercode/file.cpp:5:4: error: stray '\302' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:5: error: stray '\240' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:13: error: stray '\302' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:14: error: stray '\240' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:18: error: stray '\302' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:19: error: stray '\240' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:26: error: stray '\302' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:27: error: stray '\240' in program for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:6:1: error: stray '\302' in program if (i == HELLO) { ^ /usercode/file.cpp:6:2: error: stray '\240' in program if (i == HELLO) { ^ /usercode/file.cpp:6:3: error: stray '\302' in program if (i == HELLO) { ^ /usercode/file.cpp:6:4: error: stray '\240' in program if (i == HELLO) { ^ /usercode/file.cpp:6:7: error: stray '\302' in program if (i == HELLO) { ^ /usercode/file.cpp:6:8: error: stray '\240' in program if (i == HELLO) { ^ /usercode/file.cpp:6:14: error: stray '\302' in program if (i == HELLO) { ^ /usercode/file.cpp:6:15: error: stray '\240' in program if (i == HELLO) { ^ /usercode/file.cpp:7:1: error: stray '\302' in program break; ^ /usercode/file.cpp:7:2: error: stray '\240' in program break; ^ /usercode/file.cpp:7:3: error: stray '\302' in program break; ^ /usercode/file.cpp:7:4: error: stray '\240' in program break; ^ /usercode/file.cpp:7:5: error: stray '\302' in program break; ^ /usercode/file.cpp:7:6: error: stray '\240' in program break; ^ /usercode/file.cpp:7:7: error: stray '\302' in program break; ^ /usercode/file.cpp:7:8: error: stray '\240' in program break; ^ /usercode/file.cpp:8:1: error: stray '\302' in program } ^ /usercode/file.cpp:8:2: error: stray '\240' in program } ^ /usercode/file.cpp:9:1: error: stray '\302' in program cout << i << "\n"; ^ /usercode/file.cpp:9:2: error: stray '\240' in program cout << i << "\n"; ^ /usercode/file.cpp:9:16: error: stray '\302' in program cout << i << "\n"; ^ /usercode/file.cpp:9:17: error: stray '\240' in program cout << i << "\n"; ^ /usercode/file.cpp:10:2: error: stray '\302' in program } ^ /usercode/file.cpp:10:3: error: stray '\240' in program } ^ /usercode/file.cpp:1:1: error: 'include' does not name a type include <iostream> ^~~~~~~ /usercode/file.cpp: In function 'int main()': /usercode/file.cpp:5:7: error: 'string' was not declared in this scope for (string i = H; i < HELLO; i++) { ^~~~~~ /usercode/file.cpp:5:7: note: suggested alternative: 'struct' for (string i = H; i < HELLO; i++) { ^~~~~~ struct /usercode/file.cpp:5:23: error: 'i' was not declared in this scope for (string i = H; i < HELLO; i++) { ^ /usercode/file.cpp:5:28: error: 'HELLO' was not declared in this scope for (string i = H; i < HELLO; i++) { ^~~~~ /usercode/file.cpp:9:4: error: 'cout' was not declared in this scope cout << i << "\n"; ^~~~
Wow i got many errors🤣🤣
using namespace std in the top of the code The problem is another
#include <iostream> #include <string> using namespace std; void print(const string text) { for (int i(0); i < text.size(); i++) { for (int j(0); j <= i; j++) cout << text.at(j); cout << '\n'; } } int main() { print("HELLO"); return 0; }
Обсуждают сегодня