c++:how do i add user input in while loop? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
how do i add a user input to my code? I want to create a simple while loop program that will increment the value input by the user by 2.
#include <iostream>
using namespace std;
int main()
{
int a=2;
while(a<=100)
{
cout<< "Value of a: "<<a<<endl;
a=a+2;
}
return 0;
}

Not sure I completely understand your question, but the syntax for user input would be cin >> variable here;

Related

How can make a program typing with c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can make a program typing with c++?
For example with (char) can type a grapheme but i want type word.
char x;
x=='c';
But i want type for example 'equation'.
Someone know how can i do this?
Use a std::string. You can also use char* but i recommend you the first one. Remember that string goes within "" instead of ''.
string x; //You have to define what is 'x'.
x=="equation";
If you use char*, it behaves as an array of chars.
#include <string> // paste this line on top of the file
std::string name = "Bob"; // Use double quotes here.
if(name == "Bob")
{
std::cout << name << std::endl; // works as you expected
}

A puzzle game from codechef [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Could somebody explain the solution to this problem in cpp and how it works
Can this be made more efficient using STL?
http://www.codechef.com/problems/H1
It would very helpful if you could explain this particular logic along with its time complexity.
#include <cstdio>
using namespace std;
int board[9],q[178000]={123456789},powers[9]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
int posn,fromposn,front=-1,back=1;
char found[98765435]={0};
bool prime[18]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1};
void moveit(int p1,int p2){
int diff=board[p2]-board[p1];
posn=fromposn+diff*powers[p1]-diff*powers[p2];
int posndiv10=posn/10;
if(!found[posndiv10]){
found[posndiv10]=found[fromposn/10]+1;
q[back++]=posn;
}
}
int main(){
int t,i,d;
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
found[12345678]=1;
while(++front<back){
for(posn=fromposn=q[front],i=8;i>=0;i--){
board[i]=posn%10;
posn/=10;
}
if(prime[board[0]+board[1]]) moveit(0,1);
if(prime[board[0]+board[3]]) moveit(0,3);
if(prime[board[1]+board[2]]) moveit(1,2);
if(prime[board[1]+board[4]]) moveit(1,4);
if(prime[board[2]+board[5]]) moveit(2,5);
if(prime[board[3]+board[4]]) moveit(3,4);
if(prime[board[3]+board[6]]) moveit(3,6);
if(prime[board[4]+board[5]]) moveit(4,5);
if(prime[board[4]+board[7]]) moveit(4,7);
if(prime[board[5]+board[8]]) moveit(5,8);
if(prime[board[6]+board[7]]) moveit(6,7);
if(prime[board[7]+board[8]]) moveit(7,8);
}
scanf("%d",&t);
while(t--){
for(posn=i=0;i<8;i++){
scanf("%d",&d);
posn=posn*10+d;
}
scanf("%d",&d);
if(found[posn]) printf("%d\n",found[posn]-1);
else puts("-1");
}
return 0;
}
It first builds a table of all the solvable boards, represented as numbers, that says how many steps from the solution that board is.
Then it solves each test case by looking it up in that table.
The time complexity per test case is constant.
I doubt that there is anything in the standard library that could make it more efficient.

How do I print out a sentence using linked list in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I just started learning C++.
I want to print out a sentence or at least a word.
I could just use cout<<"hello world";
but I want to use linked list.
Is that possible?
You could do it this way, which uses a linked list and output printing a word or space at a time...
#include <list>
#include <iostream>
#include <string>
int main() {
std::list<std::string> words;
words.push_back("hello");
words.push_back(" ");
words.push_back("world");
for (auto const& word : words) {
std::cout << word;
}
std::cout << std::endl;
}

why do we need strcpy()? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
struct prac
{
int name[3];
char name1[12];
} b1, b2, c2;
main()
{
int i;
struct prac b2={1,2,3};
strcpy(c2.name1,"goodmorning");
printf("%s",c2.name1);
}
here to store the values in b2 array we just need to write b2={1,2,3} but if I want to store some values in the string c2 we need to call strcpy(), why it shows error if I write c2="goodmorning", rather than using strcpy()?
Because you can't assign to an array, only initialize it (which you do when you define the second b2 variable) or by copying to it (which you do with the strcpy call).

Is there a efficient way to do multiple test cases in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use MS Visual Studio and I am new to C++, so I am just wondering if there is an faster more efficient way to do multiple test cases instead of keep clicking CTRL+F5 and re-opening the console many times.
Like for example if I have this code
#include <iostream>
using namespace std;
void main ()
{
int x;
cout<<"Enter a number"<<endl;
cin>>x;
cout<<x*2<<endl;
}
Is there a way I could try different values of x at once and getting the results together?
Thanks
Simple work around:
while(terminating_condition_is_not_met)
{
execute_what_you_want
}
terminating condition can be EOF or max_no_of_iterations or some_sentinel_value
And for your code, I used -1 as a sentinel.
#include <iostream>
using namespace std;
void main ()
{
int x;
while(1)
{
cout<<"Enter a number"<<endl;
cin>>x;
if(x==-1)
break;
cout<<x*2<<endl;
}
}