C++ code buggy, needs fixing [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Hey guys I've got this piece of code, posted below. I've added the while loop to make sure that only numeric input is used, but when I use it, it requires me to input the number twice, or press enter and then input the number.
Output would be:
Input number : 1
1
then it would it would print the results. How can I fix this Cheers.
void Dictionary::SearchNumeric()
{
int x;
cout << "Input number : ";
while (!(cin >> x))
{
cout << "Invalid input. Try again: ";
cin.ignore(numeric_limits<streamsize>::max());
}
string searchWord = myWords[x]->word;
cout << "Word searched: " << searchWord << endl;
cout << "Definition: \n" << myWords[x]->definition << endl;
cout << "Type: " << myWords[x]->type << endl;
int wordIndex = 0;
//while (myWords[wordIndex]->word.compare(x) != 0) {
//needs to return scrabble score
wordIndex++;
//break;
//}
}

Get rid of the first cin >> x;, set the string searchWord after the while

Related

How can I program something that asks two integers from the user and displays it in the following sequence in C++? [closed]

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'm a kind of beginner in C++ and need little help here.
How can I create a C++ program which takes two integers from the user and It displays the sequence in this format using Nested Loops:
Enter the starting integer: 11
Enter the ending integer: 18
(11,11)(11,12)(11,13)(11,14)(11,15)(11,16)(11,17)(11,18)
(12,12)(12,13)(12,14)(12,15)(12,16)(12,17)
(13,13)(13,14)(13,15)(13,16)
(14,14)(14,15)
or
Enter the starting integer: 1
Enter the ending integer: 5
(1,1)(1,2)(1,3)(1,4)(1,5)
(2,2)(2,3)(2,4)
(3,3)
I wrote the code something like this before:
int startingval;
cout << "Enter starting integer: ";
cin >> startingval;
int endingval;
cout << "Enter Ending integer: ";
cin >> endingval;
int looptime;
looptime = endingval;
endingval = startingval;
for (int i = 0; i < startingval; i++)
{
cout << "(" << startingval << ", " << endingval << ")";
endingval++;
if (endingval == looptime + 1)
{
i = startingval;
}
}
return 0;
But, This is not what I need. Please Help me :)
first, you can't use any tags except that's in your question.
the only difference between the sequence you want and the normal nested loop between all elements in the array is the ending point is decreesing
I suggest you try the following code:
int start;
cout << "Enter starting integer: ";
cin >> start;
int end;
cout << "Enter Ending integer: ";
cin >> end;
for(int i=start;i<=end;i++){
for(int j=i;j<=end;j++)
cout << "(" << i << ", " << j << ")";
cout<<endl;
end--;
}
I hope be useful, Let me know if there is a problem.

New C++ Programmer. Having trouble with loops. How should I fix this program? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
For practice, I'm writing a program that takes in data from the user about the classes they took and at the end, outputs that data into a file of a neat looking transcript with a bunch of calculations such as GPA, total units, etc.
I'm using a do-while loop but it doesn't seem to be working.
I believe the problem is with the addClass variable, as even though I specified it to only ask for another class if addClass = 1, it still asks for one when I input 0. Does anyone who has more experience have a solution to this? Thank you.
//Prog: Unofficial Transcript Creator
//Modified 5-08-2018
#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
#include<string>
using namespace std;
int main() {
//Declares.
string classSubject;
int classCode;
string professorFirst, professorLast;
int classUnits;
string grade;
int addClass;
ofstream fout;
//Open the output file.
fout.open("UNOFFICIAL_TRANSCRIPT.TXT");
//Test if the file opened.
if (fout) {
cout << "The output file has been located. Please begin input of transcript data." << endl;
cout << endl;
}
else
cout << "ERROR ID107: The output file was not found. Please create a blank text document named UNOFFICIAL_TRANSCRIPT.TXT.";
//Prompt user for information.
do {
addClass = 0;
cout << "Please enter the class subject: ";
cin >> classSubject;
cout << endl;
cout << "Please enter the class code: ";
cin >> classCode;
cout << endl;
cout << "Please enter the first name of the professor: ";
cin >> professorFirst;
cout << endl;
cout << "Please enter the last name of the professor: ";
cin >> professorLast;
cout << endl;
cout << "How many units is the class worth? ";
cin >> classUnits;
cout << endl;
cout << "What grade did you get in the class? ";
cin >> grade;
cout << endl;
cout << "Would you like to add another class? Type 1 for yes or 0 for no. ";
cin >> addClass;
cout << endl;
fout << setw(12) << classSubject << classCode;
}
while
(addClass = 1);
system("pause");
return 0;
}
Replace while(addClass = 1 ) with while(addClass == 1).
The former will assign the value 1 to addClass and then check whether the value of the expression (which is the value assigned, i.e. 1) is non-zero.
Since this value is non-zero, your loop would never be able to get out of your loop.
The latter performs an equality check to see if the value of addClass is equal to 1.
IMHO, while (addClass = 1) should also raise a warning on any decent compiler( especially if all warnings are enabled ), as it is a very common mistake/typo.
Also, if it was not a typo, then now would be a good time to brush up your C++ basics.

Can you add variables inside "cout" [closed]

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'm new to C++, on the chapter 2 quiz of LearnCpp.com. I'm stuck and have a question. Can you add variables inside a std::cout statement? For example:
The program will not display my answer. The program ends as soon as the user presses enter after inputting the values. Thanks for your help ahead of time.
EDIT: Sorry for not posting the entire code. I'm also new to forums. I added the () like someone suggested. When I ran the program I think I saw it display the answer for a split second and it doesn't show that Press any key to continue. . .
#include "stdafx.h"
#include <iostream>
int main()
{
double first_value;
double second_value;
char user_operator;
std::cout << "Enter a double value: ";
std::cin >> first_value;
std::cout << "Enter a second double value: ";
std::cin >> second_value;
std::cout << "Enter one of the following (+, -, *, /): ";
std::cin >> user_operator;
if (user_operator == 43 || user_operator == 45\
|| user_operator == 42 || user_operator == 47)
switch (user_operator)
{
case 43:
std::cout << " " << (first_value + second_value) << "\n";
break;
case 45:
std::cout << " " << (first_value - second_value) << "\n";
break;
case 42:
std::cout << " " << (first_value * second_value) << "\n";
break;
case 47:
std::cout << " " << (first_value / second_value) << "\n";
break;
}
else std::cout << "Please enter a valid operator.";
return 0;
}
Yes, you can perform operations within a chain of std::ostream& operator<<(std::ostream&, T) calls. You just need to obey operator precedence and put parenthesis (()) around the expression to disambiguate in case it's necessary.
Here's a fixed Demo.

How do I get rid of an unneeded text thats from a cout in a loop? C++ (Picture) [closed]

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 8 years ago.
Improve this question
Screenshot:
Where it shows "The sum of 2 + 1 + 3 + is 6", I want to get rid of that last "+" sign after 3, as it obviously doesn't look good to show. Does anyone have any idea how to get rid of it?
Thank you in advance!
cout << "The sum of ";
for (i = 1; i <= InputLimit; i++) // This loop shows all user entered 'elements' of the array
cout << numbers[i] << " + ";
cout << " is " << sum << ". ";
Just put a conditional:
for (i = 0; i <= InputLimit; i++){ // This is just the loop to show the numbers with '+' or no.
if (i != InputLimit){
cout << numbers[i] << " + ";
}
else {
cout << numbers[i];
}
}
I hope it works for you XD

How do I stop it from looping endlessly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So below is my while loop code. Loop is endless when I enter anything other than double for cin. How do I make it so that "cout << " Invalid! Please enter the correct amount. " only comes once and that it asks for cin straight after that.?
int main ()
double pWeekdays7am_7pm;
cout << "\n Please enter the amount of electricity (kWh) produced in the following time periods. If no electricity was produced, enter \"0\"" << endl << endl;
cout << " Monday-Friday (7am-7pm) ";
cin >> pWeekdays7am_7pm;
while (pWeekdays7am_7pm < 0)
{ cout << " Invalid! Please enter the correct amount. " ;
cin >> pWeekdays7am_7pm;
cout << "Enter positive number, or 0\n";
cin >> pWeekdays7am_7pm;
if (pWeekdays7am_7pm < 0)
{
cout << " Invalid! Please enter the correct amount. ";
while (pWeekdays7am_7pm < 0)
{
cin.sync(); cin.clear(); // get rid of anything unwanted
cin >> pWeekdays7am_7pm;
}
}
Assuming you still have the problem a solution to the problem you have would be the following:
#include <limits> // require for limits
#include <iostream> // required for I/O functionality
using namespace std; // to avoid using std:: all the time.
int main (int argc, char **argv) {
double pWeekdays7am_7pm;
cout << "\n Please enter the amount of electricity (kWh) produced " <<
"in the following time periods. If no electricity was " <<
"produced, enter \"0\"" << endl << endl;
cout << "\tMonday-Friday (7am-7pm) ";
cin >> pWeekdays7am_7pm;
/*
condition to advance, we check for two things
1) if the conversion to *double* failed, hence cin.fail will return *true*
2) if the converted value is within our limits (>= 0)
*/
while (cin.fail() || pWeekdays7am_7pm < 0) {
cout << "\tInvalid value! Please enter the correct amount: " ;
/* reset the stream state */
cin.clear();
/* truncate existing contents up to new line character */
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cin >> pWeekdays7am_7pm;
}
/* finally return */
return 0;
}
The comments should be pretty straight forward to explain what this code does but should you have any more questions update here and I'll try to answer. I would suggest picking some online resources or a good C++ book and read it.
Hope this helped.