I have some questions

1)Is it right?
main()
{
int *a,*b;
cin>>&a>>&b;
}
2)How can we get a random number between -7 & 10?
x=-7+rand()%18
3)Is it right???
struct phone{
   int ph;
   string s;
};
main()
{
phone arr[10];
for(int i=0;i<10;i++){
cin>>arr[i].ph>>arr[i].s;
}
4)Do we have "string *p" like " char *p"?
5)How can we put a stack as input of a function?(make a simple example please.)
1. No. In Java, pointers are hidden/implied. In C++ they're explicit.
1
2
3
4
int main() {
    int a, b;
    cin >> a >> b;
}


2. Does that include 10?

3. std::string is a string object defined in the standard library, so you say string s, so no.

4. In much the same way a std::string is an object in the standard library, there's also std::stack. You simply instantiate one and pass it by reference. (Sorry, no example right now.)
I forgot to write it is in C++
Can anyone answer?????????
1 is wrong
for cin don't use &
What about others???
closed account (E0p9LyTq)
C++ random number generation using the C++ <random> and <chrono> libraries (a very sloppy and trivial example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <random>
#include <chrono>

int main()
{
   // obtain a seed from the system clock:
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());

   // create a generic random engine generator
   std::default_random_engine rd(seed);

   // discard some initial values for better randomization
   rd.discard(1000);

   // set a distribution range (-7 to 10 inclusive)
   std::uniform_int_distribution<int> dist(-7, 10);

   // generate a random number
   int number = dist(rd);

   std::cout << "The random number is: " << number << '\n';
}
First use Code Option to send code section

1.

you simply can try to test your code in arbitrary Compile such as g++ in Linux
and see if that code is correct or not.
i try and find this way is correct:

1
2
3
int *a,*b;
cin>> *a >> *b;
cout << *a;


try to other thing such as bellow cause error, Read Compiler Output Messages.
1
2
int *a,*b;
cin>> a >> b;


1
2
int *a,*b;
cin>> &a >> &b;

1
2
3
int *a,*b;
cin>> *a >> *b;
cout << *a;


This is NOT correct. You declare 2 pointers and don't assign them. Then when you read into them, you overwrite unknown memory--whatever is pointed to by the garbage they previously contained.

The following would be fine:

1
2
3
4
5
int i, j;
int *a = &i;
int *b = &j;
cin>> *a >> *b;
cout << *a;


Now when you read in the values they get put into specific memory locations.

However, I @OP was looking for @kbw showed in post #2.
Topic archived. No new replies allowed.