| View previous topic :: View next topic |
| Author |
Message |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Jul 09, 2008 2:24 am Post subject: [C++] Tiny App. Need Quick Fix |
|
|
I'm getting into making a questionnaire bot, But first I'd like it to say hi and whatnot. Here is what I've got so far (it obviously isn't done) :
| Code: | #include <iostream>
using namespace std;
#define NEWLINE '\n'
int main()
{
cout << "Hello There!" ;
cout << NEWLINE;
cout << "What is your name?" ;
cout << NEWLINE;
int name;
cin >> name;
cout << "Why hello there";
cout << name;
return 0;
} |
The whole NEWLINE thing, er yeah I was having errors using the \n method so yeah, ignore that. ANYWAYS, the problem is, it works fine until the very end where it then says .
Whats the problem?
_________________
Last edited by manc on Wed Jul 09, 2008 2:47 am; edited 1 time in total |
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Wed Jul 09, 2008 2:37 am Post subject: |
|
|
ok well your supposed to state your variables (name) at the top of the code before anything....another thing....u dont need to even use the /n or \n or w/e it is......just put endl; at the end of the code line. Im leanring c++ too and here ill just be nice and rewrite your code.........one second im gonna copy it from dev/c++ since its hard typing it here.............look out for my edit
_________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Jul 09, 2008 2:41 am Post subject: |
|
|
Alright thanks..I'll probably still be here.
I actually rewrote it also..here is what I got now
| Code: | #include <iostream>
using namespace std;
int name;
int main()
{
cout << "Hello There!" << endl;
cout << "What is your name?" << endl;
cin >> name;
cout << "Why hello there" << name << endl;
} |
Except now it says Why hello there0 =\?
_________________
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Wed Jul 09, 2008 3:01 am Post subject: |
|
|
hey dude, sorry to break it to you but, what your trying to do is not possible with ur knowledge so far........think about it......when u state your variables, int = a number right? so when u enter a letter it would just enter 0 (a number) instead of wat u typed
to fix this, u need to use something called strings. strings can be used for ANYTHING the user inputs, ex. 56,94,Travis,manc, . Strings store the inputted data for late use. let me show u wat u were trying to do in a new source code | Code: | #include <iostream>
#include <string>
using namespace std;
string name = ""; // "" means its nothing until the user enters something
int main ()
{
cout << "Hello, what is your name?" << endl;
cin >> name;
cout << "Hello, " << name.c_str() << endl;
system("PAUSE");
return 0;
}
|
u need the SYSTEM("PAUSE"); thing because some compilers dont pause before closing, this forces the user to press a button before it closes
something that you can do with ur knowledge so far using the same idea would be to do like "what is your favourite number" and then the user would input a number (int) or a number with a potential decimal (double)
it would look like this | Code: | #include <iostream>
using namespace std;
int main ()
{
double favnumber;
cout << "Hello, what is your favourite number? (can have decimal)" << endl;
cin >> favnumber;
cout << "So your favourite number is " << favnumber << " ? AWESOME!" << endl;
system("PAUSE");
} |
also remember to leave a space before u put the inputted message in. ex.
cout << "So your favourite number is(space)" << favnumber << "(space)? AWESOME!" << endl;
_________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed Jul 09, 2008 6:14 am Post subject: |
|
|
Uhg... Travis13 don't use system() anything! It's horrible lol. You can pause the console using the cin functions using:
| Code: | std::cin.sync();
std::cin.ignore(); |
| Code: | #include <windows.h>
#include <iostream>
int main( int argc, TCHAR* argcv[] )
{
char tszName[256] = {0};
std::cout << "Hello, please tell me your name: ";
std::cin >> tszName;
std::cout << "Why hello there, " << tszName << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
} |
The example uses char specifically due to using cout/cin, if you wish to use Unicode, you can change to wchar_t and use wcout / wcin.
_________________
- Retired. |
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Wed Jul 09, 2008 11:33 am Post subject: |
|
|
| Wiccaan wrote: | Uhg... Travis13 don't use system() anything! It's horrible lol. You can pause the console using the cin functions using:
| Code: | std::cin.sync();
std::cin.ignore(); |
| Code: | #include <windows.h>
#include <iostream>
int main( int argc, TCHAR* argcv[] )
{
char tszName[256] = {0};
std::cout << "Hello, please tell me your name: ";
std::cin >> tszName;
std::cout << "Why hello there, " << tszName << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
} |
The example uses char specifically due to using cout/cin, if you wish to use Unicode, you can change to wchar_t and use wcout / wcin. |
i can use w/e i want...system functions are very useful for other things too like system("COLOR 5f"); or system("TITLE Hello World"); and so on............there is nothing bad about it so just drop it
_________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed Jul 09, 2008 11:50 am Post subject: |
|
|
| Travis13 wrote: | | i can use w/e i want...system functions are very useful for other things too like system("COLOR 5f"); or system("TITLE Hello World"); and so on............there is nothing bad about it so just drop it |
Have you ever researched it or debugged it yourself? It's a huge waste of resources. Yes, you are free to use what you want, but I suggest not using it.
As for everything else you listed, you can do all those with other functions.
system("pause");
| Code: | std::cin.sync();
std::cin.ignore(); |
system("TITLE Hello World");
| Code: | | SetConsoleTitle( _T("Hello World") ); |
system("COLOR 5f");
| Code: | | SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), BACKGROUND_RED|BACKGROUND_BLUE|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY ); |
No reason to use system() to waste resources when you have functions that do the same thing.
Console Functions:
http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx
_________________
- Retired. |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Wed Jul 09, 2008 12:24 pm Post subject: |
|
|
Also, if you noticed Wiccan didn't use strings. I recommend using char because the less includes the better.
Also using SYSTEM anything is waving bye to any portability
_________________
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Jul 09, 2008 1:09 pm Post subject: |
|
|
Right, anyways i pretty much figured out the part i was stuck on..heres what I have now..
| Code: | #include <iostream>
using namespace std;
string name;
int a;
int main()
{
cout << "Hello There!" << endl;
cout << "What is your name?" << endl;
cin >> name;
cout << "Why hello there " << name << "!" << endl;
cout << " I'm going to present you with a series of questions.";
cout << " To answer them," <<endl;
cout << "simply press 1 or 2.";
cout << " Any other keys pressed will produce errors";
cout << " Are you ready to begin? 1.Yes 2.No" << endl;
cin >> a;
{if (a == 1)
cout << "Ok. ready to begin" ;
else if (a == 2);
cout << "Well too freakin bad";
else (a > 2);
cout << "Choice : " << a << " Is not recognizable ";
}
|
Except any variable i type for a doesn't work.
Would this be any closer?
| Quote: | cin >> a;
if(a != NULL)
if(a > 0)
if(a < 3)
{
if(a == 1)
cout << "Ok. ready to begin";
if(a == 2)
cout << "well to freakin bad";
}
else
{
cout << "Choice : " << a << " Is not recognizable ";
} |
Secondly..I'd like to add a series of questions to which you reply 1 or 2 and if something besides that is typed, give an error and loop back to the question Someone suggested case switches, is this what i should implement?
_________________
|
|
| Back to top |
|
 |
Typhoon808 Expert Cheater
Reputation: 0
Joined: 27 Mar 2008 Posts: 175 Location: Wales
|
Posted: Wed Jul 09, 2008 1:35 pm Post subject: |
|
|
Your syntax for most of the program is wrong. I suggest you look up the correct way to use if/else statements.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed Jul 09, 2008 2:24 pm Post subject: |
|
|
Something like this would suite what you are asking:
| Code: | #include <windows.h>
#include <iostream>
#include <string>
int main( int argc, TCHAR* argcv[] )
{
std::string strUsername;
int a;
std::cout << "Hello There!" << std::endl;
std::cout << "What is your name?" << std::endl;
std::cin >> strUsername;
std::cout << "Why hello there " << strUsername << "!" << std::endl;
std::cout << "I am going to preset you with a series of questions." << std::endl
<< "To answer them, press 1 or 2." << std::endl
<< "Any other keys pressed will produce errors." << std::endl
<< "Are you ready to begin? [1 = Yes, 2 = No]" << std::endl;
std::cin >> a;
if( a == 1 )
{
std::cout << "You selected 1, you are ready." << std::endl;
}
else if( a == 2 )
{
std::cout << "You selected 2, you are not ready." << std::endl;
}
else
{
std::cout << "Invalid input." << std::endl;
}
std::cin.sync();
std::cin.ignore();
return 0;
} |
_________________
- Retired. |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Jul 09, 2008 2:33 pm Post subject: |
|
|
| Wiccaan wrote: | Something like this would suite what you are asking:
| Code: | #include <windows.h>
#include <iostream>
#include <string>
int main( int argc, TCHAR* argcv[] )
{
std::string strUsername;
int a;
std::cout << "Hello There!" << std::endl;
std::cout << "What is your name?" << std::endl;
std::cin >> strUsername;
std::cout << "Why hello there " << strUsername << "!" << std::endl;
std::cout << "I am going to preset you with a series of questions." << std::endl
<< "To answer them, press 1 or 2." << std::endl
<< "Any other keys pressed will produce errors." << std::endl
<< "Are you ready to begin? [1 = Yes, 2 = No]" << std::endl;
std::cin >> a;
if( a == 1 )
{
std::cout << "You selected 1, you are ready." << std::endl;
}
else if( a == 2 )
{
std::cout << "You selected 2, you are not ready." << std::endl;
}
else
{
std::cout << "Invalid input." << std::endl;
}
std::cin.sync();
std::cin.ignore();
return 0;
} |
|
Yeah..thats it. Thanks. One more question though, What are you doing in these parameters?
| Code: | | int main( int argc, TCHAR* argcv[] ) |
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Wed Jul 09, 2008 2:42 pm Post subject: |
|
|
They are the standard parameters for the main function. They are used for command line parameters if someone loaded your application up using a parameter.
argc is the count of parameters passed when the program was started, and argcv[] is an array that holds each of the params.
The first param, argcv[0], is the path to the program.
_________________
- Retired. |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Wed Jul 09, 2008 5:34 pm Post subject: |
|
|
Suppose that they select 2(no) or something else (invalid input) besides 1(yes), how can I make it revert back to "Are you ready to begin...." line? What would come after goto? Or would i use a loop?
_________________
|
|
| Back to top |
|
 |
Fuzz Grandmaster Cheater
Reputation: 0
Joined: 12 Nov 2006 Posts: 531
|
Posted: Wed Jul 09, 2008 6:11 pm Post subject: |
|
|
Create your own functions, and call back to them when you need to in the loop.
_________________
|
|
| Back to top |
|
 |
|