Questions

Pages: 1234
1) calling main() is prohibited.
2) Do not recursively call function when you need it to run again. That is what loops are for.
3) It is logical that mail, name and password would be strings and not integers.

Just try to enter non-number in main() or when it asks you to hit 1 to log in.
http://pastebin.com/1jKf1H7J

Well?

Can you give me some ideas for some programs i could make ?
Get rid of recursive call to regis() inside regis().

Some ideas: http://www.cplusplus.com/articles/N6vU7k9E/
I have a problem:


void application2_sub(){

if(ask("Do you want to continue?(y or n)")==true){

cls();

application2();
}


}

void application2()
{

}

I could inverse those two , but in applicaton2() i have also called applicaton2_sub():
Last edited on
If you want repeating something, this will work the best:
one function which does the job. And second which asks user if they want to repeat in a loop and calls first function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void f2()
{
    std:cout << "Did something\n";
}

void f1()
{
    do {
        f2();
    } while( ask("Do you want to continue?(y or n)") )
}


int main() 
{
    f1();
}


It worked.

Can i CIN multiple numbers with a loop?

Exemple:

for(int i =1;x<=10;x++){


}

Can I CIN all the numbers loop give me?

Something like , the for loop to fill an array.
Last edited on
You can:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int array[10];
//input
for(int i = 0; i < 10; ++i)
    std::cin >> array[i];
//output backward
for (int i = 9; i >= 0; --i)
    std::cout << array[i];

/*...*/
std::vector<int> values;
int i;
while(std::cin >> i) { //while input is correct
    if(i < 0)
        break; //Stop input if negative number is entered
    values.push_back(i); //Add number in container
}

How can i CIN an random number?

Ex:

for(int x=0;x<1;x++){

std::cout<<(rand)()%100

}

cin is input stream. Somebody should enter someting into it for subsequent read. Random numbers is something program generates itself. Code your posted will work fine:
http://coliru.stacked-crooked.com/a/18b72960dcd7f4fc
I actually finished the first 5 programs of http://www.cplusplus.com/articles/N6vU7k9E/

http://pastebin.com/Ta6WsZNu

It's ok?
Last edited on
First three are working fine.

In Pancake Glutton you will have problems if there is a tie for first or last place. It should output at least one of participants.
And think how will you write program if there is at least 100 participants.

Bracketing search simply does not do what asked. It does not generate random number and line 67 accesses uninitialized variables which UB. Also whole external loop is nit needed at all as you have same loop inside.
In Pancake Glutton you will have problems if there is a tie for first or last place. It should output at least one of participants.

That's not a big problem , i can fix it.

And think how will you write program if there is at least 100 participants.


Actually.. I was trying to do some loops, but no success, can you show me one method?

Bracketing search simply does not do what asked. It does not generate random number and line 67 accesses uninitialized variables which UB. Also whole external loop is nit needed at all as you have same loop inside.


It doesn't suppose to have a random generation..That;s beacuse i asked if the random number can be read..
And i don't know another method to do this.
Last edited on
Generating random number:
C++11 way: http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
C way: http://en.cppreference.com/w/cpp/numeric/random/rand

can you show me one method?
The idea is to sort container (using std::sort) with info about participants after you enter info. Then maximum and minimum will be at he differend ends of array.

You will need to link participant and amount of pancakes. You can make custom structure and provide comparison operator:
1
2
3
4
5
6
7
8
9
10
struct participant
{
    int index;
    int pancakes;
};

bool operator<(const participant& lhs, const participant& rhs)
{
    return lhs.pancakes < rhs.pancakes;
}
Or use std::pair<int, int> which defines operators itself. Just remember to write pancakes amount into first member (to sort pancakes, not participants numbers)
http://en.cppreference.com/w/cpp/utility/pair/pair

Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <algorithm> //for std::sort
#include <array>     //for std::array
#include <functional>//for std::greater
#include <iomanip>   //for std::setw
#include <iostream>
#include <utility>   //for std::pair


int main()
{
    const std::size_t persons = 10;
    std::array<std::pair<int, int>, persons> eaten;

    for(int i = 1; i < 11; ++i) {
        std::cout << "Number of pancakes person " << i << " have eaten: ";
        int temp;
        std::cin >> temp;
        eaten[i-1] = std::make_pair(temp, i);
    }
    std::sort(eaten.begin(), eaten.end(), std::greater<std::pair<int, int>>());

    for(int i = 0; i < 10; ++i) {
        std::cout << "Person " << std::setw(2) << eaten[i].second <<
                     " have eaten " << eaten[i].first << " pancakes" << std::endl;
    }
}
Generating random number:
C++11 way: http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution


I don't quite understand ...
Can you show me a simple example ?(which can be introduced in my program)

About the example..

Again, i never used those functions.. but I will try to modify this , and use it when i need it.

I think i will start to read some books,the first one will be C++ primer , it's a good one ?


Last edited on
1) First you need a pseudo-random bit generator. This is some facility which generates a hard-to-predict sequence of bit. Mersenne twister is a good tradeoff between randomness and speed. In older C++ or C you would use rand() which is often uses subpar prng implementation.
1
2
3
4
#include <random>
//...
std::mt19937 rg(/* Read next section*/);
//...  


2) Then you need to give it a start value. Best approach is to give some really unpredictable number, but finding source of it might be non-trivia. Best approach is to use std::random_device, but it might be not supported (for example MinGW does not support it), so I would stop on std::time(): old classic, which would be enough for your purpose.
1
2
3
4
5
#include <random>
#include <ctime>
//...
std::mt19937 rg(std::time(nullptr)); //Create random generator rg seeded with     //...
}


3) Now you need something to transform sequence of random bits into number. That is distribution is for. as you need some number from diapason, std::uniform_int_distribution is needed. It has the template parameter denoting return type and takes two values: min and max value of range it needs to generate:code]// Generate numbers from 1 to 100
// ↓ ↓
std::uniform_int_distribution< > dis(1, 100);
//Template argument is int ↑
//by default, so we do not need to explicitly mention it[/code]

Then you need to call distribution with generator as parameter: dis(rg) 
You can wrap it in function nicely:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <random> //Random stuff
#include <ctime> //std::time
#include <iostream>

int rand_num(int from, int to)
{
    //Static is needed to seed our prng only once
    //and all function calls will share generator
    static std::mt19937 rg(std::time(nullptr));
    //distribution is recreated each call because function might be called with different parameters
    std::uniform_int_distribution<> dis(from,  to);
    return dis(rg); //return result of generation
}

int main()
{
    for(int i =0; i < 20; ++i)
        std::cout << rand_num(1, 100) << '\n';
}
46
93
96
11
73
91
20
82
45
83
85
19
36
24
3
66
15
93
85
70
http://ideone.com/3bYd9n
I don't understand how to put this in my bracketing program, how can the computer know what the random number will be.. the computer need to know the number to tell me if my guess it's to high or to low...
the computer need to know the number to tell me if my guess it's to high or to low...
It generates a random number and then uses it to tell if your guess is high or low. As you write int random_number_honest = 4, you can write int random_number = generate_random(1, 100);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <random>
#include <ctime>
#include <string>

int rand_num(int from, int to)
{
    static std::mt19937 rg(std::time(nullptr));
    std::uniform_int_distribution<> dis(from,  to);
    return dis(rg);
}

void playAsGuesser()
{
    int number = rand_num(1, 100); //Generate random number and remember it.
    int attempts = 0;
    while(true) {
        ++attempts;
        std::cout << "Enter number: ";
        int guess;
        std::cin >> guess;
        if (guess == number)
            break;
        else if (guess < number)
            std::cout << "too low\n";
        else
            std::cout << "too high\n";
    }
    std::cout << "Congratulations! You guessed number in " << attempts << " attempt\n";
}

int main()
{
    playAsGuesser();
}
I was reading a book , and i find the string function(never used it before), and i did a exercise from that book...



int main()
{
do
{
cls();

cout << "Enter two names..." << endl;

string v1,v2;

cin>>v1>>v2;

if(v1.size()==v2.size())
{
cls();
cout<<v1<<" and " <<v2<<" have the same number of letters"<<endl;
}
else if(v1.size()>v2.size())
{
cls();
cout<<v1<<" has more characters than "<<v2<<endl;
}
else if(v1.size()<v2.size())
{
cls();
cout<< v2 << " has more characters than " << v1 <<endl;
}
}
while(ask("Do you want to continue?(Y or N)"));
}


In .size , what can i put in?
In .size , what can i put in?
Nothing. It is typical no-arguments function which returns number of characters in string.
http://en.cppreference.com/w/cpp/string/basic_string/size

http://en.cppreference.com/w/cpp/string/basic_string
I have a problem , i started working with files and decided to make a program who stores each day a number of Push Ups, and at the end of each day is showing you how many you made , and which is the average number of push ups the user did...


ofstream pushups("PushUps.txt");

pushups.open();

PushUps << day << ' ' << push;

pushups.close();

When i close the program the day and the number of push ups remain, but when i start the program second time it's overwriting the first day...
What's wrong ?
Pages: 1234