Code: Show/Hide #include <iostream>
#include <windows.h> #include <stdlib.h> #include <string> using namespace std; int main() { SetConsoleTitle("TESTING BETA PROGRAM"); SetCursorPos(512,512); string pass; cout<<"Enter non numeric password ( type quit and press enter to quit): "; cin>>pass; if (pass==hi) { cout<<"nice if you want to quit type quit and then press enter"; } else if (pass==quit) { cout<<"closing.."; return 0; } else { cout<<"wrong password type quit to quit this program"; } return main(); } |
Purge wrote: |
Since you're comparing strings you should use strcmp. |
Purge wrote: |
Since you're comparing strings you should use strcmp. |
Mine GO BOOM wrote: |
[..]
string is different than char*, so strcmp should not be used here. |
hellzlaker wrote: |
is there any advantages between
switch statement and if (x==0){} ? |
Cerium wrote: |
You can always just use str[] (or str[0]) for use with strcmp, where "str" is an instance of string. |
Code: Show/Hide #include <stdio.h>
#include <string> /* prints words from [-delta, delta). */ void disp_mem(void* p, int delta) { unsigned long* q = (unsigned long*) p; int i; for (i = -delta; i < 0; ++i) { printf("%08x ", q[i]); } puts(""); for (i = 0; i < delta; ++i) { printf("%08x ", q[i]); } puts(""); return; } void draw_str(std::string& s, const char* msg) { printf("-- (%s) --\nDRAWING STRING: \"%s\"\n", msg, s.c_str()); printf("str ptr: 0x%08x\n", *(unsigned int*) &s); printf("Memory at [t-7,t+7):\n"); disp_mem(*(void**) &s, 7); } int main() { std::string s("Hel"); std::string t("Foobar foobar foo bar barf ew"); draw_str(t, "before s = t"); s = t; printf("std::string size: %d\n", sizeof(std::string)); draw_str(s, "s = t"); draw_str(t, "t initialized"); s[3] = '@'; draw_str(s, "s, after s[3] = '@'"); draw_str(t, "t, after s modified"); s += "Okaaaaay."; draw_str(s, "s += Okaaaay."); s += s; s += s; s += s; s += s; s = "foo"; draw_str(s, "s 16-upled, s = foo"); s = ""; draw_str(s, "s = \"\""); s.clear(); draw_str(s, "s.clear()"); s.resize(0); draw_str(s, "s.resize(0)"); s.reserve(0); draw_str(s, "s.reserve(0)"); s.reserve(1); draw_str(s, "s.reserve(1)"); } |