C++, If statement problems
I am trying to make an IF statment to check the value i enter against the pre set value.
This is my code
#include <iostream>
using namespace std;
char name[200],title[200];
int main()
{
cout<< "Enter your name: ";
cin.getline(name,200);
if(name != "CD")
{
main();
}
cout<<"Enter the title: ";
cin.getline(title,200);
cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";
cin.get();
return 0;
}
But no matter what i enter as my name , it always goes back to the main function.
I tried to put the value im checking against in a var, but that caused an error.
char check[200];
check = "CD";
and i also tried putting in , cin.ignore(); after getting the input, but that didnt work. It still done the same as the first code.
#include <iostream>
using namespace std;
char name[200],title[200];
int main()
{
cout<< "Enter your name: ";
cin.getline(name,200);
cin.ignore();
if(name != "CD")
{
main();
}
cout<<"Enter the title: ";
cin.getline(title,200);
cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";
cin.get();
return 0;
}
Any help would be good. Thanks, CD
When you post code, DISABLE SMILEYS. The only times you would not use a semi-colon to terminate a line in C++ would be these:
- In the actual testing portion of a conditional statement or loop
- When you're assigning a multi-line string or overlap your line of code to the next line
There's a mistake, to start with… whether it would cause the issue you're seeing or not is yet to be seen. Also, if you're going to be running a function over and over again, it's a better idea to write another function and use that… don't loop main(), cuz it looks goofy and could cause more issues than you really need.
I think i need to read more of the tutorial for C++.
Ok, i have made this now
#include <iostream>
using namespace std;
char name[200],title[200];
char error();
int main()
{
cout<< "Enter your name: ";
cin.getline(name,200);
if(strcmp (name, "CD") != 0)
{
cout<< "Error\n";
}
else
{
cout<<"Enter the title: ";
cin.getline(title,200);
cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";
}
cin.get();
return 0;
}
sharpskater80 wrote: You can scope from std, just make sure you include <iostream>.
Wow, that's crazy… because I know I'm rusty on my C++, but every site you go to on basic variable declarations insists that you have <string> included. However, it is probably just a case of where they are ALL wrong and you're right.
Anyways… does it really matter what he includes? It's the meat of his code that is being critiqued.
With regards to not including the string header file, you have to include it! There is an exception to this however. If you are using the microsoft IDEs to compile your programs then when you leave out headers they will try to find and include the ones you are intending to use. This is very bad practice. Always include the ones you need and dont let microsoft do it for you.
sharpskater80 wrote: Just wanted to let him know that string/cstring are C library headers.
Curious… I was under the impression that <Cstring> and <string.h> were used for string functions, and that <string> was a class that inherited std. Guess I should get back into C++ at some point… been meaning to.
Zephyr_Pure wrote: [quote]sharpskater80 wrote: Just wanted to let him know that string/cstring are C library headers.
Curious… I was under the impression that <Cstring> and <string.h> were used for string functions, and that <string> was a class that inherited std. Guess I should get back into C++ at some point… been meaning to.
[/quote]
He's using std. He doesn't need it. It works fine without including it with g++.
@ Coder Disaster: A.) I see why you picked that nick.
B.) Use strings. Much simpler and it's one big feature in C++ that is better than in C.
C.) Just use cin >> instead of cin.getline
D.) You should really be able to figure this out.
E.) Because I'm feeling nice:
#include <iostream>
using namespace std;
string name;
string title;
int Prog()
{
cout << "Enter your name: ";
cin >> name;
if(name != "CD" || name=="")
{
cout << "Error\n";
return 0;
}
else
{
cout << "Enter the title: ";
cin >> title,200;
cout << "Your name is: " << name << "\nAnd You are: " << title << "\n";
}
cin.get();
return 1;
}
int main()
{
while((Prog())==0)
{
}
return 0;
}
Zephyr_Pure wrote: [quote]sharpskater80 wrote: Just wanted to let him know that string/cstring are C library headers.
Curious… I was under the impression that <Cstring> and <string.h> were used for string functions, and that <string> was a class that inherited std. Guess I should get back into C++ at some point… been meaning to.
[/quote]
#include <cstring> and <string.h> are synonymous. Usually, you either prefix c to use the standard C headers (cstdlib,cstdio,ctime) or you affix it with .h (stdio.h, stdlib.h,time.h). Look around on cplusplus.com (it doesn't implicitly say that, but you can tell when it talks about the C functions and my compiler doesn't complain either).
This is now my code , advanced a bit , but still no strings ,:( need to learn them next i think.
#include <iostream> //include the input output stream
using namespace std; // using the namespace std
char name[200],password[200]; //define the vars for username and password
int main() // start the main function
{
cout<< "Enter your name: ";// print the message ,enter your name
cin.getline(name,200); // get what the user inputs
if(strcmp (name, "username") != 0)// check the input against the predefined username, if wrong
{
cout<< "Error, try again\n";//print error message, then new line
main();//restart the main function
}
else//if the username is correct
{
cout<< "Enter your password: ";// print the message enter your password
cin.getline(password,200); //get the password from the input
if(strcmp(password,"password") != 0)//check the password, and if wrong do
{
cout<< "Error, try again\n"; // print error message and new line
main();//restart function
}
else//if password is correct
{
cout<<"Loading custom settings...\n";// print loading settings
system("title Your handle");//change the title to what your handle
system("color 0a");//change the text color to green with black background
cout << "Welcome handle.\n";//print the message Welcome ...
//system("cd Program Files\Mozilla Firefox\ ");
cout<< "Opening pre-set web pages.\n";//open web pages
system("start firefox.exe http://www.computerhope.com/overview.htm");
system("start firefox.exe http://www.hellboundhackers.org");
system("start firefox.exe http://www.cprogramming.com/tutorial/");
cout<< "Web pages opened\n";//print message when oppened
}
}
cin.get();
return 0;
}
Also i was going to add a function if you get it wrong that , i will do later, but it works so im happy :D
[quote]Coder Disaster wrote: I am trying to make an IF statment to check the value i enter against the pre set value.
This is my code
#include <iostream>
using namespace std;
char name[200],title[200];
int main()
{
cout<< "Enter your name: ";
cin.getline(name,200);
if(name != "CD")
{
main();
}
cout<<"Enter the title: ";
cin.getline(title,200);
cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";
cin.get();
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char **argv){
string name,title;
cout<<"Enter your name: ";
getline(cin,name);
if(strcmp(name.c_str(),"CD")!=0){
main();
}
cout<<"Enter the title: ";
getline(cin,title);
cout<<"Your name is: "<<name<<"\n And your are: "<<title<<"\n.";
cin.get();
return 0;
}
You might have to tweak it some, but it should work. You were confusing C syntax with C++ syntax. I am to lazy to code the C example, but lookip printf, scanf, strcmp, and the .c_str() method for the ability to use C functions on C++ strings.
hacker2k wrote: He's using std. He doesn't need it. It works fine without including it with g++.
@ Coder Disaster: A.) I see why you picked that nick.
B.) Use strings. Much simpler and it's one big feature in C++ that is better than in C.
C.) Just use cin >> instead of cin.getline
D.) You should really be able to figure this out.
Alright. Then, what is the purpose of <string>?
Pwnzall wrote: #include <cstring> and <string.h> are synonymous. Usually, you either prefix c to use the standard C headers (cstdlib,cstdio,ctime) or you affix it with .h (stdio.h, stdlib.h,time.h). Look around on cplusplus.com (it doesn't implicitly say that, but you can tell when it talks about the C functions and my compiler doesn't complain either).
Yes, yes, I was aware of this… next time, I will be sure to put a / instead of "and" so you won't have to respond. No thanks on the site.
Zephyr_Pure wrote: [quote]sharpskater80 wrote: Just wanted to let him know that string/cstring are C library headers.
Curious… I was under the impression that <Cstring> and <string.h> were used for string functions, and that <string> was a class that inherited std. Guess I should get back into C++ at some point… been meaning to.
[/quote] From what I have found, It is safe to assume that it is.
<string> is for the std::basic_string template
(from which std::string is created).
http://bytes.com/forum/thread62119.html
http://www.cplusplus.com/reference/string/string/
<string>
String class
String objects are a special type of container, specifically designed to operate with sequences of characters.
Unlike traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a class with many built-in features to operate with strings in a more intuitive way and with some additional useful features common to C++ containers.
The string class is an instantiation of the basic_string class template, defined in <string> as:
typedef basic_string<char> string;
Member functions
(constructor) Construct string object (constructor member)
operator= String assignment (public member function)
Iterators:
begin Return iterator to beginning (public member function)
end Return iterator to end (public member function)
rbegin Return reverse iterator to reverse beginning (public member function)
rend Return reverse iterator to reverse end (public member function)
Capacity:
size Return length of string (public member function)
length Return length of string (public member function)
max_size Return maximum size of string (public member function)
resize Resize string (public member function)
capacity Return size of allocated storage (public member function)
reserve Request a change in capacity (public member function)
clear Clear string (public member function)
empty Test if string is empty (public member function)
Element access:
operator[] Get character in string (public member function)
at Get character in string (public member function)
Modifiers:
operator+= Append to string (public member function)
append Append to string (public member function)
push_back Append character to string (public member function)
assign Assign content to string (public member function)
insert Insert into string (public member function)
erase Erase characters from string (public member function)
replace Replace part of string (public member function)
copy Copy sequence of characters from string (public member function)
swap Swap contents with another string (public member function)
String operations:
c_str Get C string equivalent (public member function)
data Get string data (public member function)
get_allocator Get allocator (public member function)
find Find content in string (public member function)
rfind Find last occurrence of content in string (public member function)
find_first_of Find character in string (public member function)
find_last_of Find character in string from the end (public member function)
find_first_not_of Find absence of character in string
find_last_not_of Find absence of character in string from the end (public member function)
substr Generate substring (public member function)
compare Compare strings (public member function)
For those interested , this is what i have
#include <iostream> //include the input output stream
using namespace std; // using the namespace std
char name[200],password[200],addi[200],final['200']; //define the vars for username and password
char start[] = "start firefox.exe ";
int login();
char newpage();
int main() // start the main function
{
if(login())//if password is correct
{
system("cls");
cout<<"Loading custom settings...\n";// print loading settings
system("title Coder Disaster");//change the title to what your handle
system("color 0a");//change the text color to green with black background
system("cls");
cout << "Welcome Coder Disaster.\n";//print the message Welcome ...
//system("cd Program Files\Mozilla Firefox\ ");
cout<< "Opening pre-set web pages.\n";//open web pages
system("start firefox.exe http://www.computerhope.com/overview.htm");
system("start firefox.exe http://www.hellboundhackers.org");
system("start firefox.exe http://www.cprogramming.com/tutorial/");
cout<< "Web pages opened\n";//print message when oppened
newpage();
}
cin.get();
return 0;
}
int login()
{
cout<< "Enter your name: ";// print the message ,enter your name
cin.getline(name,200); // get what the user inputs
if(strcmp (name, "username") != 0)// check the input against the predefined username, if wrong
{
cout<< "Error, try again\n";//print error message, then new line
system("cls");
login();//restart the main function
}
else
{
cout<< "Enter your password: ";// print the message enter your password
cin.getline(password,200); //get the password from the input
if(strcmp(password,"password") != 0)//check the password, and if wrong do
{
cout<< "Error, try again\n"; // print error message and new line
system("cls");
login();//restart function
}
else
{
return 1;
}
}
}
char newpage()
{
cout<< "Open new page\n";
cout<< "http://";
cin.getline(addi,200);
strcpy(final,start);
strcat(final,addi);
system(final);
cout<< "Page opened\n";
newpage();
}
hacker2k wrote: So that you can just include the data-type (I'm guessing). I don't know much about C++ though, so I'm most likely wrong.
The purpose of <string> is to define the String class as an inheritant of std so that it can use its functions. If <string> doesn't need to be included by the compiler, then it's likely that the compiler is including it for you or the standard library tries to resolve a descendant by searching the lib paths.
Oh… and no one's "likely wrong" when there's a chance to have an intelligent discussion. Any of us could be, but it doesn't matter… there's a goal here.
Pwnzall wrote: From what I have found, It is safe to assume that it is. <snip>
Well, the majority of that could've stayed linked, but it's okay. Yes, string inherits std… which doesn't work the other way around. std does not automatically include string; however, if you make a string declaration, it's possible that either the compiler or the library is "looking for its children".
So, basically, it boils down to a question of whether it's a convention of the language or of modern compilers. Thank you both for contributing to this intelligent discussion.
{
cout<< "Open new page\n";
cout<< "http://";
cin.getline(addi,200);
strcpy(final,start);
strcat(final,addi);
system(final);
cout<< "Page opened\n";
newpage();
}```
You are making a recursive call, and for some reason not returning a character.
and then new information arrives
uh, christ
> You can scope from std, just make sure you include <iostream>.
thanks for putting me in my place
stop being a hot shot, no one cares
sharpskater80 wrote: I think this is just a big misunderstanding of where the C++ string class is included from, <iostream>.
No… it's not. Are all the children of a higher class included when a higher class is included? If you said 'yes', you're a moron. If you include a lower class and it inherits from a higher class, then maybe… but, including a lower class that inherits a higher one is the only way to guarantee that the lower class is included. Case in point: You create a class that inherits from std… Will it be automatically included? No. You'd have to include it manually.
<cstring> and <string.h> are for C++ and C compilers respectively, but are both C string libraries.
Those are function libraries… where is the String class actually defined?
yours31f wrote: ok I will attempt to settle this,
If you #include <iostream> and using namespace std; string and some if the more basic math.h is inherited. Now if he wanted to find the length of the of the string or any other function like that then yes he would have to #include <string>
Well, you tried… and failed. The majority of the string functions are not included in <string> but, rather, in <cstring> / <string.h>. It's ironic that you say iostream includes this string class declaration functionality but, when it comes down to checking that statement's credibility, it seems like all sources agree that string does not automatically get included and, alternatively, must be included as a descendant of std.
Arguments?