Black Magic 15 Junior Poster

I tried that but still get errors,

#include <iostream>
using namespace std;

std::string aLine;

int main()
{
    system("title Simple use of the switch statement.");
    system("Color C");
    
    unsigned int digit;
    
    cout << "Please enter a digit between 1 & 5 : ";
    cin >> digit;
    
    switch(digit)
    {
                 case '1': 
                      cout << endl << "You selected 1!";
                           break;
                           
                 case '2': 
                      cout << endl << "You selected 2!";
                           break;
                           
                 case '3': 
                      cout << endl << "You selected 3!";
                           break;
                           
                 case '4': 
                      cout << endl << "You selected 4!";
                           break;
                           
                 case '5': 
                      cout << endl << "You selected 5!";
                           break;
    }
    
    cin.getline( aLine );
    
    return 0;
}

Or have i used the code incorrectly?

Black Magic 15 Junior Poster

Thanks, i get a couple of errors though, do i need to include a header or something else?

Error's:

C:\Users\ayre\Desktop\Learning C++\forLoop.cpp:18: error: no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline()'
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:582: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/istream:399: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]

Execution terminated

Black Magic 15 Junior Poster

Hey, i was thinking these may help me if I made these little programs etc, but i've came to a situation, I've go no error but have to use

cin.get();

twice, here's the whole program..

#include <iostream>
using namespace std;

int main()
{
    system("title Simple use of if else if loops.");
    system("Color C");
    
    int digit;
    
    cout << "Enter a positive digit: ";
    cin >> digit;
    
    if( digit > 0 )
    {
             cout << endl << "Thank you!";
    }
    
    else if( digit < 0 )
    {
         cout << endl << "I said postive!"; 
    }
    
    cin.get();
    cin.get();
    
    return 0;
}

The reason i have to use it twice is that if I only use it once the program just opens and closes straight away, I am not sure but I am certain it is because i have made the program for the user to console input the variable digit, am I correct with my theory?

Black Magic 15 Junior Poster

Never mind..

Black Magic 15 Junior Poster

Hey, I was just wondering what was the simplest way to "real" world type, like open wordpad and you could watch it actually typing, thanks in advance. (Hopefully the answer is not writing to file etc)

Black Magic 15 Junior Poster

Sorry for this late reply but i've changed to firstColum, secondColum and thirdColum again, i'll post code when home and i've added options like if output = XXX you win $1, i'm planning to make it $0.10 a go and the total changes etc etc :)

Black Magic 15 Junior Poster

So are you saying i should only call srand once and use one variable for random number or..?

EDIT: I think i cracked that bit!

#include <iostream>

using namespace std;

int main()
{
    unsigned int firstColum, secondColum, thirdColum;
    
    /////////////////////////////////////////////////////////
    
    srand ( time(NULL) );
    
    firstColum = rand() % 2 + 1;
    
    /////////////////////////////////////////////////////////
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    cin.get();
}
Black Magic 15 Junior Poster

I went to the beach at the weekend and played on one inpeticular game, the one were you pull the lever and XOO or w.e comes out, i was going to make one but thought i would ask if this was the best solution..

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    unsigned int firstColum, secondColum, thirdColum;
    
    /////////////////////////////////////////////////////////
    
    firstColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    secondColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    thirdColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    if( secondColum == 1)
    cout << "X";
         else
    cout << "O";
    
    if( thirdColum == 1)
    cout << "X";
         else
    cout << "O";
    
    getch();
}

EDIT: I've noticed i usually get same output

Black Magic 15 Junior Poster

Dev-C++ works on vista, I'm not sure about Visual (I've never used it) Yes Normal C++ code.

Black Magic 15 Junior Poster

Welcome to the world of C++!

1: I use bloodshed's Dev-C++ (Free)

2: www.cplusplus.com has some good text tutorials and if you search on youtube there is some good beginner tutorials.

I reccomend you buy a book though, good luck

Black Magic 15 Junior Poster

EDIT: Thanks for posting link to book, asked parents and might be allowed book soon, btw is it a book or e-book?

Black Magic 15 Junior Poster

I have got c++ in 21 days though have only read the first 5 days

Black Magic 15 Junior Poster

Thanks, btw on the if statement i tried the "s but got a error, changed to 's and got same error as before

Black Magic 15 Junior Poster

Are you on about my case statement?

Black Magic 15 Junior Poster

Hey. I get bored so muck around with c++ and i have made this:

/* My shop program*/

#include <iostream>
#include <fstream>
#include <conio.h>
#include <time.h>

using namespace std;

int main()
{
    double quantity;
    unsigned int itemNo;
    
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    
    ofstream myfile;
    
    cout << "Please enter the item number : ";
    cin >> itemNo;
    
    switch(itemNo)
    {
                  case '0100':
                       cout << endl << "Quantity? : ";
                       cin >> quantity;
                       cout << 0.5 * quantity;
                       
                       myfile.open ("shop.txt", ios::ate | ios::app);
                       myfile << itemNo << " * " << quantity << asctime (timeinfo);
                       myfile.close();
                       break;
    }
    

    
    getch();
}

It all compiles fine, and everything but i get a [WARNING] multi-character character constant.

What happens is the program executes and displays "Please enter the item number : " so i enter the number and then press enter but then i think getch(); is executed, can someone help me with this little mistake, thanks.

Black Magic 15 Junior Poster

Is it at a school? cause at my school we arent supposed to download anything but we do anyways

Even though the thread had been solved it's at a community centre and you can't download things, and at school you can't download anything at all, you only have a couple of programs and controlled internet

Black Magic 15 Junior Poster

Thank you all! I'm just sorry i was no help :(

Black Magic 15 Junior Poster

I'm really sorry but i cannot see what i have to do, i've tried "tweaking" things around and still get 000

Black Magic 15 Junior Poster

I edited my code and used hammerheads code, and when i inputted 123 it outputted 000.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    unsigned int number, number2, number3;
    
    cout << "Enter number to reverse : ";
    cin >> number;
    
    number  = number % 10;
    number  = number/10;
    number2 = number % 10;
    number  = number/10;
    number3 = number % 10;
    
    cout << endl << number << number2 << number3;
    
    getch();
}
Black Magic 15 Junior Poster

Sorry about cout, i've altered the %'s to be 10, but i get how i get the last digit, it's just the other two i'm not sure how to get even with your solutions i'm not very good

Black Magic 15 Junior Poster

I am not 100% sure on your solutions, so far i've just made this :

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    unsigned int number, number2, number3;
    
    cout << "Enter number to reverse : ";
    cin >> number;
    
    number  = number % 10;
    number2 = number % 100;
    number3 = number % 1000;
    cout << endl << number, number2, number3;
    
    getch();
}

If you enter 123 it only outputs 3, am i on the right lines?

Black Magic 15 Junior Poster

Thanks, i searched and came across this website, http://www.dreamincode.net/forums/showtopic38967.htm

I ran the program and got an error, though is that the simplest solution?

Black Magic 15 Junior Poster

Hey,
I was just thinking and came across this idea and wanted to know if it could be done, say if the user enters 321, my program would output 123, 450, 054 etc etc

Could you please show me how this could be achieved thanks.

Black Magic 15 Junior Poster

Hey, i have made this little guessing game and was wondering how i could clear the screen every time to ask the question if the guess is wrong, though keeping line's 15 and 17 displayer all the time, i've tried but got msessed up programs etc, please help me adjust my code

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    unsigned int randomNumber = 0;
    unsigned int guess = 0;
    unsigned int guesses = 0;
    
    srand ( time(NULL) );
    randomNumber = rand() % 1000 + 1;
    
    cout << "Welcome to my simple guessing game!" << endl << endl;
    
    cout << "Guesses : " << guesses << endl << endl;
    
    while(guess != randomNumber)
    {
    
    cout << endl << endl <<"Guess what number i'm thinking of ( 0 - 1000 ) : ";
    cin >> guess;
    
    guesses++;
    
    if (guess > randomNumber)
    cout << endl << "Number too big!";
    
    if (guess < randomNumber)
    cout << endl << "Number too small!";
    
    if (randomNumber == guess)
    {
                     
    cout << endl << "Correct!" << endl << "You guessed in " << guesses;
    cout << "guesses";
    
    system("PAUSE>nul");
    
    }

}
}
Black Magic 15 Junior Poster

Thanks guys, now i was wondering now if i should make it so the other player cannot make your hp a negative number, how could this be done? Please reply.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 50 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 50 + 1; 
         p1hp -= p2attack;
         cout << "You hit " << p2attack << "!" << endl << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    
    }
    

}
getch();
}
Black Magic 15 Junior Poster

I've changed it to: p1hp -= p2attack;
But p1hp still stays 99

Black Magic 15 Junior Poster

Ok thank you, sorry for the mistake :S

Black Magic 15 Junior Poster

EDIT: I've got a while loop in my code, just now when player 1 goes to have his/her second attack the hp is reset to 99,

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 99 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 99 + 1; 
         cout << "You hit " << p2attack << "!" << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    p1hp -= attack;
    }
    

}
getch();
}
Black Magic 15 Junior Poster

Hey,
I have noticed when you use srand or rand you write:

rand() % 99 + 1

I was wondering about the 1, is it just tradition to use it or is it from 1-99?
Just because i was wondering how i could make it between say 20 and 40 etc

Black Magic 15 Junior Poster

Edit: fixed that error, still working on game

Black Magic 15 Junior Poster

Thanky you for poiting out the variable parts, now for the game bit, would i use a while loop for when the hp is > 0?

Black Magic 15 Junior Poster

Thank you, i still get:
invalid suffix
expected ; before int

Black Magic 15 Junior Poster

Hey, i was just going to start making a game where srand made a random number between 1&99 and that is the power you would hit of someone else, the one to defeat the other person won,

I got some prefix errors etc :

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int 1attack, 2attack, 1hp, 2hp;
    char attack;
    
    cout << "Player 1, Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         1attack = rand() % 99 + 1; 
         cout << "You hit " << 1attack << "!";
    break;
    
    case default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    getch();
}

I was going to put this on to ask for help with how to do things with the game so i'll leave this thread for help with the errors and maybe some help with the game :)?

Black Magic 15 Junior Poster

Thanks, but the real confusion is how i would select the answer if the names are inputted again

Black Magic 15 Junior Poster

We do not like to be referred to as "geeks"

And basicly it does not matter what you put in the if statement because you are saying

if(f<0.7)
cout << "C+";

else <- anything else at all
cout << "C++";

So basicly if you input a number less than 0.7 you would get C+, else any number eg 0.7,7,77 etc etc
would give you a outout of C++

EDIT: you should change void main() -> int main()
clrscr is undeclared,

seriously you need to modify your code

Salem commented: Well said! +15
Black Magic 15 Junior Poster

The title says it all, i was just wondering if i made a simple text based game what would i use? Like I've heard about things called sockets, ports etc? Just in curiosity

Black Magic 15 Junior Poster

Can someone please help??

Black Magic 15 Junior Poster

Ok, thank you, we're not allowed to download anything lol so ahh well thanks anyways

Black Magic 15 Junior Poster

Hey,
I was just wondering if i could write my C++ code on notepad and run it etc?
I am asking this because atm i'm at a community centre and on a computer, but not allowed to download anything, so can i?

reply asap please

Black Magic 15 Junior Poster

Thank You!

Black Magic 15 Junior Poster

Hey, Yet again i was just mucking around on DEV-C++ and made this code..

#include <conio.h>
#include <iostream>

using namespace std;

unsigned long Double(unsigned long doubleMe)
{
         return (2 * doubleMe);
}

int main()
{
    unsigned long numberToDouble;
    unsigned long doubledNumber;
       
    cout << "Enter a number you would like to double : ";
    cin >> numberToDouble;
    
    doubledNumber = numberToDouble;
    Double(doubledNumber);
    
    system("cls");
    
    cout << numberToDouble << " Doubled = " << doubledNumber;
    
    getch();
}

It is meant to cout the original number and then the doubled number, but if i enter 7, it cout's 7 doubled = 7

Help please?

I tried declaring it before using it but it said something like too many arguments,do i need to declarelike unsigned long Double(..); or not?

Black Magic 15 Junior Poster

BUMP

Black Magic 15 Junior Poster

Hey,
Was bored so *TRIED* to make this simple countdown program but i'm just not sure what to put in the for loop?

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    int countDown;
    
    cout << "Enter a number to count down from : ";
    cin >> countDown;
    
    for(int i )
    {
            cout << i;
            Sleep(1000);
            system("cls");
    }
}

I've tried int i == countDown etc, but i don't know help please

Black Magic 15 Junior Poster

Yeah ino, I'm learning flash in I.T atm but i know more about computers than my I.T teacher.. I've took something called CIDA for year 10 and thats to do with computers etc :) BUT STILL NO PROGRAMMING LESSONS

Black Magic 15 Junior Poster

@ William - Cool, i wish we could get programming classes at our age at school though :/

Black Magic 15 Junior Poster

Hello, I have decided to save the names to file etc, thanks to help from my other post i have gotten this code

#include <time.h>
#include <conio.h>
#include <fstream>
#include <iostream>


using namespace std;

int main()
{
    ofstream myfile;
    
    char firstLover[20];
    char secondLover[20];
    
    unsigned int lovePercent;
    
    cout << "Enter The First Lovers Name: ";
    cin  >> firstLover;
    
    cout << endl << "Enter The Second Lovers Name: ";
    cin  >> secondLover;
    
    srand ( time(NULL) );
    
    lovePercent = rand() % 100 + 1;
    
    cout << endl << "Your Love Percentage Is : " << lovePercent << "%!";
    
    myfile.open ("lover_names.txt", ios::ate | ios::app);
    myfile << firstLover << " " << secondLover << " " << lovePercent << "%\n";
    myfile.close();
    
    getch();
}

Now, I don't know how i am going to make it so the compiler can see if two names are the same, and if so, give the same percentage, help please, thanks

Black Magic 15 Junior Poster

Thank you all, sorry that i did not do as much as i could, sorry :(

Black Magic 15 Junior Poster

Thanks william! Now that bit works, just i still get the "2" on every line, can i get rid of that?

Black Magic 15 Junior Poster

Sorry i read wrong,

I got errors with your code, it said missing ) before ios, so then i put ("lover...")(ios::..); etc and still got errors,

Then i put..

myfile.open ("lover_names.txt"); ios::ate;
    myfile << firstLover << " " << secondLover << " " << lovePercent << "%\n\n" << ios::ate;
    myfile.close();

But the same thing is happening, no multiple lines etc!?

Black Magic 15 Junior Poster

Thanks but i'm still stuck, where would i put ios::ate?

myfile.open ("lover_names.txt");
    myfile << ios::ate << firstLover << " " << secondLover << " " << lovePercent << "%\n\n";
    myfile.close();

This does the same as before, only keeps one thing in but this time it always has a 2 infront of the name etc, please correct me and show me where to put w.e