Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
char restart = 'y';
while (restart == 'y')
{
cin >> restart;
cout << "great keep playing\n";
}
cout << "thanks for playing\n";
When I run this code it also displays the code outside of the loop as well, I even tried using a break statement but it didn`t work. How do I fix this?
You could write the loop like this:
while (cin >> restart && restart == 'y')
{
cout << "great keep playing\n";
}
The main issue I see is that you get the value of restart on the 'cin' line, then display 'great keep playing' on the next line, without first checking to see what the value of 'restart' is. You need an if() statement around the 'great keep playing' line, so it will only be displayed if you entered 'y'.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have just started working in Visual Studio Code.
I am facing a problem that my code is not working in VS Code even though it is working in an online compiler. A basic "hello world" program is running fine in VS Code.
I am using the mingwin g++ compiler running the code using VS Code (Ctrl + Shift + B).
My code is given below. If I uncomment the cout statement, the complete code will work fine.
#include <iostream>
using namespace std;
int main()
{
int t;
//cout << "hello-world";
cin >> t;
cout << t;
}
The reason why you only see a blinking cursor waiting for an input from user is because your program runs cin first before the cout. If you uncomment line 6 in your code, you will see "hello-world" displayed first before the blinking cursor. I see no error in your code even you uncomment that one.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hello everyone as the title says as I run through my code everything works fine, it loops correctly but somehow ends up always picking up the default case before restarting the loop.
I'll post my code through pastebin since it's quite long:
https://pastebin.com/TxvH3MhJ
while(myScore != "new" || myScore != "NEW" || myScore != "New"){
switch(s2){
case 1 :
switch(s3){
// code
}
default :
cout<<("\nSomething went wrong, please restart the program.");
break; //this statement works correctly.
}
default : //this statement is always brought up each time i type "new", i also added a cout s2 to check if actually somehow the variable was changing mid code but it prints out correctly.
cout<<("\nSomething went wrong please restart the program.");
cout<<s2;
break;`
p.s. I'm a beginner please bear in mind that probably the code is very rudimental although it is doing the job. at the moment the code just works if you enter 1s because i'm just working out the logic.
break; breaks out of the innermost switch (or loop) only. So you need to place it after inner switches too (unless you would need fall-through), like:
switch(s1) {
case 1:
switch(s2) {
...
}
break; // <== HERE
case 2:
...
break;
default:
....
break; // optional
}
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 3 years ago.
Improve this question
goto is useful FOR BEGINNERS but not recommended !!!!
I am editing my post as I got the right answer on this website.
In this program where user enters subject marks, (after entering marks)he/she is asked whether he/she wants to enter more marks of subjects after declaring in the initial stage that of how many subjects he wants to enter numbers of and if he/she replies Y then program asks him to enter marks again. Look I am a university student of 1st semester and I found goto easier to make my program go to initial stage of program after using so many loops. All I need is to not use goto but use another loop so how can I do this (problem solved by eerorika who answered me).
#include<iostream>
using namespace std;
int main (){
int subjec;
retran:
cout<<"please enter number of subjects : " ;
cin>>subjec;
int marks[subjec];
for ( int u=0;u<subjec;u++){
cout<<"enter marks of subject "<< u+1 << " ";
cin>>marks[u];
}
char q='Y';
cout<<"do you want TO ENTER MORE MARKS : "<<endl;
cout<<"enter \"Y\" for Yes and \"N\" or any other character for No : ";
cin>>q;
while (q=='Y')
goto retran;
return 0;
Here is a request if you can tell me how I can go to initial stage of program again when user press Y without using goto statement.
GOTO statements are useful ?
Yes. But not for this use case.
how I can go to initial stage of program again when user press Y
There's a control flow structure for going back and repeating. It is called a loop. An example:
do {
// do some stuff
cin>>q;
} while(q=='Y');
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want my program to be able to remember where it left off in a .txt file in order to proceed to the next input upon reiteration through a loop.
For instance, a text file containing:
Apples
Bananas
Oranges
would be accessed through a function GetItem() that appends the next file input into a vector of items. How do I make the function add Apples the first time, Bananas the second time, and Oranges the third iteration? As of now, each call to GetItem() keeps adding the first element to the vector, giving a vector containing:
Apples
Apples
Apples
Because the file keeps opening from the beginning. Any help would be appreciated.
This is a simplified version of lengthy amounts of code that I could include, but would distract from the main purpose of the question. If the code is needed, I would be happy to include it.
vector<Item*> AddItemToInventory(vector<Item*> inventory) {
if (inptLctn == 'f') {
inptFile.open("TestFood.txt");
if (!inptFile.is_open()) {
cout << "Could not open file." << endl;
exit(1);
}
inptFile >> usrInptName;
inptFile >> usrInptQnty;
inptFile >> usrInptExpr;
inptFile >> usrInptPrice;
}
prdc = new Produce;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
prdc->SetPrice(usrInptPrice);
inventory.push_back(prdc);
return inventory;
}
Open the file before you use it and close it after you are done using it. The problem is that you are continually closing the input file so when you re-open it, it starts at the beginning again.
You should only open and close your input file once.
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 7 years ago.
Improve this question
when I try to compile a simple program like this:
// i/o example
#include <iostream>
using namespace std;
int main()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i * 2 << ".\n";
return 0;
}
it starts, but when I put the integer value and press ENTER, it closes. I really don't know why it happens since it works good when I use Dev C++. And if I want to just compile my program I need to press ctrl+f5? Thank you.
What is likely happening is that the computer returns the two cout lines and then runs return 0, shutting down the program.
Since there is no pause, it just executes the code and exits, without waiting for further user input. This is what is giving the appearance of the program not working well.
If you put a breakpoint, or run the code in debug mode, or add a pause, your code will show the output as well.
And for future reference, you'll want to keep your questions to just one question per question. The Ctrl + F5 question can be found via Google or by searching SO.