Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Help] C++ Output

butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Hey there,

I am new to coding. I started following tuts like yesterday so don't be too hard on me.

I made this little thingy and wanted to have the user input 3 random words, and then display them in all 6 possibility's.
But I can't find the good way to display them the way I want.

I just got problems with the output. An example will do, I don't need spoonfeeding. (advanced stuff will probably do too as long as it's included with a bit of explanation).

This is what I have so far:
PHP:
#include <iostream>
#include <string>

using namespace std;

int main() {

     cout << "Hello there, i'd like you to type 3 random words" << endl;
     
     string word1;// Random word 1
     string word2;// Random word 2
     string word3;// Random word 3
     
     cin >> word1;//reads input
     cin >> word2;//reads input
     cin >> word3;//reads input
     
     cout << "Ok. This magical piece of pro-ness software \nshows you your three words in ALL POSSIBLE ORDERS! \nIsn't that great? =D" << endl;
     cout << word1, word2, word3 << endl;// All 6 possibilitys listed.
     cout << word1, word3, word2 << endl;
     cout << word2, word1, word3 << endl;
     cout << word2, word3, word1 << endl;
     cout << word3, word1, word2 << endl;
     cout << word3, word2, word1 << endl;
     
     system("Pause");// Pauses .bat file
     return 0;
}
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
You need to separate variables to cout with <<, not commas.

This will do: cout << word1 << ", " << word2 << ", " << word3 << endl;

Btw, you can also read the three words in one line: cin >> word1 >> word2 >> word3;
 
Last edited:
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Thanks! It works now =]

Next tut! xD

---------- Post added at 06:52 PM ---------- Previous post was at 05:54 PM ----------

I'll post here before I spam the forum lol.

The purpose was to make the entire car show (so to nullify the characters that have special meanings for the compiler)
And I added statements to it. I know the car wont fully show, but I wanted to fix the statements first.

Error:
Code:
Car.cpp `yes' undeclared (first use this function)

PHP:
#include <iostream>
#include <string>

using namespace std;

int main() {
    string answer;
    
    cout << "Want me to show you a pretty piece of art? [Yes/No]" << endl;
    
    cin >> answer; // reads input
    
    if (answer == yes) { // equals yes
        cout << "                 _..------.,._              " << endl;
        cout << "              ,-; .------.'-._`-.           " << endl;
        cout << "          __.'_/ /______/     ``-`.         " << endl;
        cout << "    _.-;'`` --_  ;     =,   .-'``\". `\/|   " << endl;
        cout << "  .'--'  .-\"-.\| |      |  / .---. `-' |   " << endl;
        cout << "  `-..__/ .-. \__'_-__-_____/ .-. \___/     " << endl;
        cout << "        \ '-' /             \ '-' /         " << endl;
        cout << "         '-.-'               '-.-'          " << endl;
    } else if (answer == no) {// equals no
      cout << "Well, thats too bad for you since you'll never see this piece of art again." << endl;
    } else {// if all other statements are false
      cout << "Only use Yes or No. Otherwise you fail." << endl;
    } 
    system("Pause");
    return 0;
}
 
Last edited:
Joined
Jul 26, 2006
Messages
3,632
Reaction score
1,007
PHP:
#include <iostream>
#include <string>

using namespace std;

int main() {
    string answer;
    
    cout << "Want me to show you a pretty piece of art? [Yes/No]" << endl;
    
    cin >> answer; // reads input
    
    if (answer == "yes") { // equals yes
        cout << "                 _..------.,._              " << endl;
        cout << "              ,-; .------.'-._`-.           " << endl;
        cout << "          __.'_/ /______/     ``-`.         " << endl;
        cout << "    _.-;'`` --_  ;     =,   .-'``\". `\/|   " << endl;
        cout << "  .'--'  .-\"-.\| |      |  / .---. `-' |   " << endl;
        cout << "  `-..__/ .-. \__'_-__-_____/ .-. \___/     " << endl;
        cout << "        \ '-' /             \ '-' /         " << endl;
        cout << "         '-.-'               '-.-'          " << endl;
    } else if (answer == "no") {// equals no
      cout << "Well, thats too bad for you since you'll never see this piece of art again." << endl;
    } else {// if all other statements are false
      cout << "Only use Yes or No. Otherwise you fail." << endl;
    } 
    system("Pause");
    return 0;
}

The input is a string and you should treat it as such!
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Thanks! Sorry, in JS i never used strings with an If statement.
---
This is the end result

PHP:
#include <iostream>
#include <string>

using namespace std;

int main() {
    string answer;
    
    cout << "Want me to show you a pretty piece of art?" << endl;
    
    cin >> answer; // reads input
    
    if (answer == "yes" || "Yes") { // equals yes
        cout << "                 _..------.,._              " << endl;
        cout << "              ,-; .------.'-._`-.           " << endl;
        cout << "          __.'_/ /______/     ``-`.         " << endl;
        cout << "    _.-;'`` --_  ;     =,   .-'``\". `\\/|   " << endl;
        cout << "  .'--'  .-\"-.\\| |      |  / .---. `-' |   " << endl;
        cout << "  `-..__/ .-. \\__'_-__-_____/ .-. \\___/     " << endl;
        cout << "        \\ '-' /             \\ '-' /         " << endl;
        cout << "         '-.-'               '-.-'          " << endl;
    } else if (answer == "no" || "No") {// equals no
      cout << "Well, thats too bad for you since you'll never see this piece of art again." << endl;
    } else {
      cout << "Only use Yes or No. Otherwise you fail." << endl;
    } 
    system("Pause");
    return 0;
}
 
Last edited:
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
Code:
if (answer == "yes" || "Yes") { // equals yes

Should be

Code:
if(answer == "yes" || answer == "Yes")
And same with No.
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Oh thanks. Lol, i didn't notice up until now that it didn't work properly lol. It always showed the car.
----
I'm calling it a day. Ima make my homework and go to bed. I got some new challenging exercises for tomorrow. Do you guys mind if I post the things I made here every once in a while? Well, technically it is bumping for nothing lol =P.

Btw, Is there any other way to execute C++ else then using .bat? Or is that advanced stuff =P.

Thanks for your help =]
 
Last edited:
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
Which IDE are you using?

You can use CodeBlocks or Dev-C++, they're both user-friendly and easy to use.
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
I'm currently using Dev-C++
 
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
Under the execute menu, there should be something like "Compile and Run". I don't see why you'd need a batch file in the first place.
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
I ment, After compiling you can execute it and it opens a commandprocessor like file (MS-DOS right?) It's being executed in there. Is there any other way to execute whatever I did? I don't really botter using MS-DOS for now, but i'd like to know how to get an actual 'program' look for the future.
 
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
Oh yes, once you finish learning C++ (classes, inheritance, friendship, templates, and so on), then you can directly move on to the Qt framework. You'll get to do windowed apps as soon as you start with Qt :)

Btw: MS-DOS is gone since windows NT (or windows 2000, correct me), you're using the windows command line, which is kind of an emulator to MS-DOS :D
 
Last edited:
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Thanks =],
Still seems like far away but I'm totally going to do my best lol.
Looking forward to make some advanced shiz =].
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Hi,

I was doing a Tut till i thought to myself: Isn't there an easier way to do this? I had to give the user options which he could choose from. But Can i make them like, click-able? instead of having the user type in the option they want? I got this and instead of doing the answer == thing all the time, it would be easier for me (and the user) to click the option.

*It's in dutch but it's supposed to be a very simple calculator like thingy.

PHP:
#include <iostream>
#include <string>

using namespace std;

int main() {
    string answer;
    int a1, a2;
    

    cout << "Wil je optellen, aftrekken, vermenigvuldigen, delen of modulo rekenen?" << endl;
    cin >> answer;
    
         if (answer == "optellen" || answer == "Optellen") {
            cout << "Typ 2 cijfers in en ik zal ze voor je optellen" << endl;
            cin >> a1;
            cin >> a2;
            cout << "De uitkomst van " << a1 << " + " << a2 << " is: " << a1 + a2 << endl;
         } else if (answer == "aftrekken" || answer == "Aftrekken") {
            cout << "Typ 2 cijfers in en ik zal ze voor je optellen" << endl;
            cin >> a1;
            cin >> a2;
            cout << "De uitkomst van " << a1 << " - " << a2 << " is: " << a1 - a2 << endl;
         } else if (answer == "vermenigvuldigen" || answer == "Vermenigvuldigen") {
            cout << "Typ 2 cijfers in en ik zal ze voor je vermenigvuldigen" << endl;
            cin >> a1;
            cin >> a2;;
            cout << "De uitkomst van " << a1 << " * " << a2 << " is: " << a1 * a2 << endl;
         } else if (answer == "delen" || answer == "Delen") {
            cout << "Typ 2 cijfers in en ik zal ze voor je delen" << endl;
            cin >> a1;
            cin >> a2;
            cout <<"De uitkomst van " << a1 << " / " << a2 << " is: " << a1 / a2 << endl;
         } else if (answer == "modulo" || answer == "modulo rekenen" || answer == "Modulo" || answer == "Modulo rekenen" || answer == "Modulo Rekenen") {
            cout << "Type 2 cijfers in ik zal ze voor je modulo rekenen" << endl;
            cin >> a1;
            cin >> a2;
            cout << "De uitkomst van " << a1 << " % " << a2 << " is: " << a1 % a2 << endl;
         } else {
           cout << "Kies een van de volgende mogelijkheden: optellen | aftrekken | vermenigvulidgen | delen | modulo rekenen" << endl;
         }
    system("pause");
}
 
Last edited:
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
You will not be able to take advantage of clicking unless you have a graphical user interface (GUI), and you cannot code one yourself right now. But that's why I recommended Qt to you. Try to make the calculator in Qt. They do that exact exercise even in a university course for GUI programming.

One thing, though. Even if the user is able to click-choose, your task will still be coding the =='s...
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
Thanks,

Yah, i know i'd still have to do the == thingy, but i wouldn't have to add all those possibilities with capital characters and such.
 
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
Use something like this, to convert the user input to lower case:


PHP:
string tolowercase(string original_string) {
    string return_string = ""; // initialize our return value
    for(i=0; i<original_string.size(); ++i) // loop through each character of the original string
        return_string[i] = tolower(original_string[i]); // C++, for some reason which i find rather illogical, doesn't know how to convert a whole string to lower case, only character by character. That's why we have to use a loop and insert the converted string char by char
    return return_string;
}


Then you can do if(tolowercase(userinput) == "optellen"), etc.

Sometimes I ask myself why the C++ "string" class is missing some of the basic things.
Why isn't its constructor overloaded? Wy couldn't you do string blahblah(7); ?
Why doesn't it have toLower(), toUpper() functions?
 
Last edited:
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
You can use something like tolower() to make the user input lowercase? then you only have to check for the lowercase.

Or do:
1. Optellen
2. Aftrekken

and make them fill in 1,2,etc?

Use something like this, to convert the user input to lower case:


PHP:
string tolowercase(string original_string) {
    string return_string = ""; // initialize our return value
    for(i=0; i<original_string.size(); ++i) // loop through each character of the original string
        return_string[i] = tolower(original_string[i]); // C++, for some reason which i find rather illogical, doesn't know how to convert a whole string to lower case, only character by character. That's why we have to use a loop and insert the converted string char by char
    return return_string;
}


Then you can do if(tolowercase(userinput) == "optellen"), etc.

Sometimes I ask myself why the C++ "string" class is missing some of the basic things.
Why isn't its constructor overloaded? Wy couldn't you do string blahblah(7); ?
Why doesn't it have toLower(), toUpper() functions?

Thanks, those are good solutions, but I guess it works for now.
@lifetaker, I could add that, but I don't want to since I can't make it myself yet. I just started with functions =] so that's a little bit to advanced for me atm.

---------- Post added at 08:05 PM ---------- Previous post was at 07:09 PM ----------

Ok, i got to functions and found some exercises.
This is what it wants me to do.
Code:
First, write a unit test and then the function. Write your function so that the unit test passes, do not get ahead of things.

1. Write a function that inherits two numbers as parameters and the average of those two numbers as a return value returns. Use broken numbers. Write unit tests for the following situations:

    * If both numbers are equal, the answer is the same as the entered numbers (for example, the average of 5 and 5 5);
    * If both numbers are opposite each other, the answer is equal to 0 (for example, the average of 5 and -5 to 0);
    * As one of the two numbers is 0, then the answer is half the other number (for example, the average of 0 and 6 3).

This is what I got so far:
PHP:
#include <iostream>

using namespace std;
// Avarage
bool AvaTest() {
     float one, two;
     
               if ((one == two) != one){
                 return false;
               
               } else if ((one == -two) != 0.0) {
                 return false;
               
               } else if ((0.0, two) != (0.0 * two) / 2) {
                 return false;
               
     //if test is succesfull
     return true;
     }
}
//UnitTest
void UnitTest() {
     // variable 
	bool mySuccesful = true;

	// executing test 
	mySuccesful &= AvaTest();

	// Tests successfully executed? 
	if (mySuccesful == true) {
		// yes, result to screen 
		cout << "Unit tests succesvol afgerond.";
	} else {
		// no, result to screen 
		cout << "Unit tests niet geslaagd!";
	}
}

int main() {
     UnitTest();
     system("Pause");
}


Code:
`one' undeclared (first use this function) 
`two' undeclared (first use this function)

I tried declaring them like this:
PHP:
bool AvaTest(float one, float two) {

Gives errors.

I tried adding
PHP:
bool AvaTest() {
     float one, two;
     ...

Works, however it says the test failed.
How do I declare them without putting a static value for it?

Can someone help me, or rather give me a little push in the right direction?
 
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
You need to pass parameters (the numbers) to the function.

This way your function proto becomes:

PHP:
bool AvaTest(float one, float two)
Also, NEVER declare variables without assigning them a value (like you did here):

PHP:
float one, two;
Unless of course if you are sure that you will give a value sooner or later.
Reason is, the operating system (that manages the memory) isn't polite: when you ask him for some space in memory (ie a variable), it doesn't reset what you are asking for. Which means, if anything was in that memory block before (from some other program) then that's what you'll get.
Example:
PHP:
#include <iostream>

using namespace std;
int main() {
    int a;
    cout << a << endl;
    return 0;
}

The result will be completely random.

So anyway, back to our function.

A function can take parameters (of any type).
This is how you write a function in C++:

PHP:
ReturnType FunctionName(TypeOfArg1 Arg1, TypeOfArg2 Arg2, TypeOfArg3 Arg3, TypeOfArg4 Arg4 [etc.]) {
    Function contents
    return (Something of ReturnType);
}

In our case:
PHP:
bool AvaTest(float one, float two) {
    // (we'll do all we want with one and two here)
}
int main() {
    // blah blah blah...
    bool mySuccessful = AvaTest(1, 2); // we passed the parameters 1 and 2 to AvaTest
}
Why are you complicating yourself with this?
PHP:
    mySuccesful &= AvaTest();
& is a bit-to-bit operator, doubt that's what you're looking for.
 
Last edited:
Back
Top