Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++, If statement problems


ghost's Avatar
0 0

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


ghost's Avatar
0 0

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:

  1. In the actual testing portion of a conditional statement or loop
  2. 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.


ynori7's Avatar
Future Emperor of Earth
0 0

Why are you using character arrays instead of strings?


ghost's Avatar
0 0

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; 
}



ynori7's Avatar
Future Emperor of Earth
0 0

Just #include <string> and then you can declare variables of 'string' type. Then you can replace your current getline lines with something that looks like this: getline(cin, variable name);

EDIT: What's the 'char error();' for? You never call it up.


ghost's Avatar
0 0

Oh yer, i deleted it now, was going to make it an error function , but decided not to.


yours31f's Avatar
Retired
10 0

just my 2 sense, 1)use string instead of char[]; 2) you don't have to declare it. 3) they were right, make a separate function and call it multiple times in main.

any more questions, feel free to pm me, I'll be on for a few more hours messing with php and mysql.


ynori7's Avatar
Future Emperor of Earth
0 0

yours31f wrote:

  1. you don't have to declare it.

Don't have to declare what?


yours31f's Avatar
Retired
10 0

#include <string> is not needed.


ghost's Avatar
0 0

ynori7 wrote: [quote]yours31f wrote:

  1. you don't have to declare it.

Don't have to declare what?[/quote]

yours31f wrote: #include <string> is not needed.

Declaring is what you do when you're making variables… and, IIRC, <string> is needed to make string vars in C++.


ghost's Avatar
0 0

You can scope from std, just make sure you include <iostream>.


ghost's Avatar
0 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.


ghost's Avatar
0 0

No, it doesn't really matter. Just wanted to let him know that string/cstring are C library headers. I understand the problem is him using one style of function for the other style of strings.


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

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.


ghost's Avatar
0 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]

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 &lt;iostream&gt;

using namespace std;
string name;
string title;
int Prog()
{
 cout &lt;&lt; &quot;Enter your name: &quot;;
 cin &gt;&gt; name;
 if(name != &quot;CD&quot; || name==&quot;&quot;)
 {
   cout &lt;&lt; &quot;Error&#92;n&quot;;
   return 0;
 }
 else
 {
 cout &lt;&lt; &quot;Enter the title: &quot;;
 cin &gt;&gt; title,200;

 cout &lt;&lt; &quot;Your name is: &quot; &lt;&lt; name &lt;&lt; &quot;&#92;nAnd You are: &quot; &lt;&lt; title &lt;&lt; &quot;&#92;n&quot;;
 }
 cin.get();
 return 1;
}
int main()
{
        while((Prog())==0)
        {
        }
        return 0;
}

ghost's Avatar
0 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).


ghost's Avatar
0 0

This is now my code , advanced a bit , but still no strings ,:( need to learn them next i think.

#include &lt;iostream&gt; //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&lt;&lt; &quot;Enter your name: &quot;;// print the message ,enter your name 
 cin.getline(name,200);  // get what the user inputs
 if(strcmp (name, &quot;username&quot;) != 0)// check the input against the predefined username, if wrong
 {
   cout&lt;&lt; &quot;Error, try again&#92;n&quot;;//print error message, then new line
   main();//restart the main function
 }
 else//if the username is correct
 {    
  cout&lt;&lt; &quot;Enter your password: &quot;;// print the message enter your password
  cin.getline(password,200);    //get the password from the input
  if(strcmp(password,&quot;password&quot;) != 0)//check the password, and if wrong do 
  {
   cout&lt;&lt; &quot;Error, try again&#92;n&quot;;     // print error message and new line                  
   main();//restart function
  }
  else//if password is correct
  {                       
  cout&lt;&lt;&quot;Loading custom settings...&#92;n&quot;;// print loading settings
  system(&quot;title Your handle&quot;);//change the title to what your handle
  system(&quot;color 0a&quot;);//change the text color to green with black background
  cout &lt;&lt; &quot;Welcome handle.&#92;n&quot;;//print the message Welcome ...
  //system(&quot;cd Program Files&#92;Mozilla Firefox&#92; &quot;);
  cout&lt;&lt; &quot;Opening pre-set web pages.&#92;n&quot;;//open web pages
  system(&quot;start firefox.exe http://www.computerhope.com/overview.htm&quot;);
  system(&quot;start firefox.exe http://www.hellboundhackers.org&quot;);
  system(&quot;start firefox.exe http://www.cprogramming.com/tutorial/&quot;);
  cout&lt;&lt; &quot;Web pages opened&#92;n&quot;;//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


ghost's Avatar
0 0

[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 &lt;iostream&gt;

using namespace std;
char name[200],title[200];
int main()
{
 cout&lt;&lt; &quot;Enter your name: &quot;;
 cin.getline(name,200); 
 if(name != &quot;CD&quot;)
 {
     main();    
 }    
 cout&lt;&lt;&quot;Enter the title: &quot;;
 cin.getline(title,200);   

 cout&lt;&lt;&quot;Your name is: &quot;&lt;&lt; name &lt;&lt; &quot;&#92;nAnd You are: &quot;&lt;&lt; title &lt;&lt; &quot;&#92;n&quot;;   
 cin.get();   
 return 0;   
}

#include &lt;iostream&gt;
#include &lt;cstring&gt;
using namespace std;

int main(int argc, char **argv){
string name,title;
cout&lt;&lt;&quot;Enter your name: &quot;;
getline(cin,name);
if(strcmp(name.c_str(),&quot;CD&quot;)!=0){
     main();
}
cout&lt;&lt;&quot;Enter the title: &quot;;
getline(cin,title);
cout&lt;&lt;&quot;Your name is: &quot;&lt;&lt;name&lt;&lt;&quot;&#92;n And your are: &quot;&lt;&lt;title&lt;&lt;&quot;&#92;n.&quot;;
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.


ghost's Avatar
0 0

Still mixing them.. Just use operator == to compare.


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

huh?


ghost's Avatar
0 0

Zephyr_Pure wrote: Alright. Then, what is the purpose of <string>?

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.


ghost's Avatar
0 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] From what I have found, It is safe to assume that it is.

&lt;string&gt; 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/
&lt;string&gt;

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 &lt;string&gt; as:

typedef basic_string&lt;char&gt; 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)



ynori7's Avatar
Future Emperor of Earth
0 0

Zephyr_Pure wrote: Alright. Then, what is the purpose of <string>?

The way I remember it, some compilers have you #include <string.h> and some you use #include <string>. I think they assume it's a '.h'. In visual c++ you dont put a '.h' on any of the header files.


yours31f's Avatar
Retired
10 0

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>


ghost's Avatar
0 0

I think this is just a big misunderstanding of where the C++ string class is included from, <iostream>.

#include &lt;iostream&gt;
int main()
{
std::string s(&quot;Thursday&quot;);
}

<cstring> and <string.h> are for C++ and C compilers respectively, but are both C string libraries.


ghost's Avatar
0 0

For those interested , this is what i have

#include &lt;iostream&gt; //include the input output stream

using namespace std; // using the namespace std
char name[200],password[200],addi[200],final[&#39;200&#39;]; //define the vars for username and password
char start[] = &quot;start firefox.exe &quot;;
int login();
char newpage();
int main() // start the main function
{
  if(login())//if password is correct
  {   
  system(&quot;cls&quot;);                        
  cout&lt;&lt;&quot;Loading custom settings...&#92;n&quot;;// print loading settings
  system(&quot;title Coder Disaster&quot;);//change the title to what your handle
  system(&quot;color 0a&quot;);//change the text color to green with black background
  system(&quot;cls&quot;);   
  cout &lt;&lt; &quot;Welcome Coder Disaster.&#92;n&quot;;//print the message Welcome ...
  //system(&quot;cd Program Files&#92;Mozilla Firefox&#92; &quot;);
  cout&lt;&lt; &quot;Opening pre-set web pages.&#92;n&quot;;//open web pages
  system(&quot;start firefox.exe http://www.computerhope.com/overview.htm&quot;);
  system(&quot;start firefox.exe http://www.hellboundhackers.org&quot;);
  system(&quot;start firefox.exe http://www.cprogramming.com/tutorial/&quot;);
  cout&lt;&lt; &quot;Web pages opened&#92;n&quot;;//print message when oppened
  newpage();
  }
 cin.get();   
 return 0; 
}

int login()
{
 cout&lt;&lt; &quot;Enter your name: &quot;;// print the message ,enter your name 
 cin.getline(name,200);  // get what the user inputs
 if(strcmp (name, &quot;username&quot;) != 0)// check the input against the predefined username, if wrong
 {
   cout&lt;&lt; &quot;Error, try again&#92;n&quot;;//print error message, then new line
   system(&quot;cls&quot;);
   login();//restart the main function
 }
else
{  
  cout&lt;&lt; &quot;Enter your password: &quot;;// print the message enter your password
  cin.getline(password,200);    //get the password from the input
  if(strcmp(password,&quot;password&quot;) != 0)//check the password, and if wrong do 
  {
   cout&lt;&lt; &quot;Error, try again&#92;n&quot;;     // print error message and new line     
   system(&quot;cls&quot;);                
   login();//restart function
  }   
  else
  {
   return 1;    
  }  
}    
}
  
char newpage()
{
  cout&lt;&lt; &quot;Open new page&#92;n&quot;;
  cout&lt;&lt; &quot;http://&quot;;
  cin.getline(addi,200);
  strcpy(final,start);
  strcat(final,addi);
  system(final);
  cout&lt;&lt; &quot;Page opened&#92;n&quot;;  
  newpage();     
}    



ghost's Avatar
0 0

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.


ghost's Avatar
0 0

Ok problem with the code i just posted, after a few seconds (logged in, and loaded pages) its keeps running the newpage function, it waits about 2 seconds then runs it, and continues.

Does anyone know why?


ghost's Avatar
0 0
{
cout&lt;&lt; &quot;Open new page&#92;n&quot;;
cout&lt;&lt; &quot;http://&quot;;
cin.getline(addi,200);
strcpy(final,start);
strcat(final,addi);
system(final);
cout&lt;&lt; &quot;Page opened&#92;n&quot;;
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 &lt;iostream&gt;.
thanks for putting me in my place
stop being a hot shot, no one cares

ghost's Avatar
0 0

Yer, but why is it doing it after a few seconds , and do i need to return anything?


ghost's Avatar
0 0

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?