How to ask for user input in nested loops? - c++

I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.
There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.
int main()
{
int NumsToCal = 5;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
} while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}

Xsami is absolutely right. You only need to include one more line like:
cin>>NumstoCal;
Though it won't be bad to change the way you output stuff for a bit more clarity.
Here is my code:
https://ideone.com/BXREP9

The only thing that you have to do is read NumsToCal again, and you have to do something like this after cin >> WantMore;
if ( WantMore == 'y' )
{
Num1 = 0;
Num2 = 1;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << Num1 << " " << Num2 << " " ;
}
This is my code: http://ideone.com/a8um5Z

Related

Trouble using count function

Below if my code that is supposed to ask for user for 2 numbers and output the sum of all even numbers between them. I am only having trouble using the count function as I don't believe I am setting it right and google has only helped me so far
#include <iostream>
using namespace std;
int main() {
int num1, num2, sum;
while(num1 > num2) {
cout << "Enter 2 numbers seperated by a space. " << endl;
cout << "First number must be smaller then second number. " << endl;
cin >> num1 >> num2;
cout << endl;
}
if(num1 % 2 == 0)
count(== num1);
else
count(== num1 + 1);
while(count(<= num2)) {
sum = sum + count;
count = count + 2;
}
cout << "The sum of the even intergers between " << num1 << "and " << num2
<< " = " << sum << endl;
return 0;
}
There is no count() function in your code, nor is there a standard count() function in the standard C++ library that does what you want (though std::accumulate() comes close). Nor do you really need such a function anyway, a simple loop will suffice.
Try something more like this:
#include <iostream>
#include <limits>
using namespace std;
int main() {
int num1, num2, sum = 0;
do {
cout << "Enter 2 numbers seperated by a space. " << endl;
cout << "First number must be smaller then second number. " << endl;
if (cin >> num1 >> num2) {
if (num1 < num2) break;
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ā€˜\nā€™);
}
cout << endl;
}
while (true);
for (int num = num1; num <= num2; ++num) {
if ((num % 2) == 0)
sum += num;
}
cout << "The sum of the even integers between " << num1 << " and " << num2 << " = " << sum << endl;
return 0;
}
If you want to omit num1 and num2 from the sum, simply change the loop accordingly:
for (int num = num1 + 1; num < num2; ++num) {

How do I change a char's values?

I've created a program and it runs, however there are two problems. 1) Char doesn't change values as it should. 2) One of my total variables is stuck on one.
I've tested the code multiple times, and the char deptID is stuck on 'B'. I've tried going through the workflow and it's stuck on the value. Just to make sure, I wrote a cout line to check it throughout the workflow. Regardless of what I input, it's stuck on 'B'.
2) The variable TechTotal is seemingly stuck on 1. I've tested it using different values as well. I also went ahead and used a cout line to determine the value throughout the workflow to no success. I've made sure the variables are correct in calculating the variable. Both are correct.
Here's my main code:
int main(int argc, char** argv) {
welcomeScreen();
long int empID;
int TechAccAvg, TechTixAvg;
int BusTotal, TechTotal, BusAccAvg, BusTixAvg;
char deptID;
for (int i=0; i < 2; i++)
{
cout << "What department are you apart of?\n";
cin >> deptID;
if (deptID = 'B')
{
auto Averages = gatherData(deptID);
BusTixAvg = std::get<0>(Averages);
BusAccAvg = std::get<1>(Averages);
cout << BusTixAvg << endl;
cout << BusAccAvg << endl;
BusTotal = BusTixAvg + BusAccAvg;
cout << "Bus Total: " << BusTotal << endl;
}
else if (deptID = 'T')
{
auto TechAverages = gatherData(deptID);
TechTixAvg = std::get<0>(TechAverages);
TechAccAvg = std::get<1>(TechAverages);
cout << TechTixAvg << endl;
cout << TechAccAvg << endl;
TechTotal = TechTixAvg + TechAccAvg;
cout << "Tech Total: " << TechTotal << endl;
}
}
cout << "Tech: " << TechTotal << endl;
cout << "Business: " << BusTotal << endl;
summaryReport(TechTotal, BusTotal);
goodByeScreen();
return 0;
}```
` std::tuple<int, int> gatherData (char dept)
{
tuple <double, double> Averages;
int employeeNum=0, TotalTix=0, TotalAcc=0, trafficTickets=0, accidents=0;
long int empID=0;
double TixAverage=0, AccAverage=0;
char deptID;
cout << dept << endl;
cout << "How many employees do you have?\n";
cin >> employeeNum;
for(int j = 0; j < employeeNum; j++)
{
cout << "Please enter your employees ID number\n";
cin >> empID;
cout << "How many tickets did they have this year?\n";
cin >> trafficTickets;
TotalTix += trafficTickets;
cout << "How many accidents did they have this year?\n";
cin >> accidents;
TotalAcc += accidents;
}
TixAverage = TotalTix / employeeNum;
AccAverage = TotalAcc / employeeNum;
cout << "Department: " << dept << endl;
cout << "Total employees: " << employeeNum << endl;
cout << "Total tickets: " << TotalTix << endl;
cout << "Total Accidents: " << TotalAcc << endl;
Averages = make_tuple (TotalTix, TotalAcc);
return Averages;
}```
This is used to create the tuple that is used in determining Totals for both 'B' and 'T' depts.
Fixing both the char dept and the TechTotal would fix the entire program, I think. Those are the only things holding the program back. I've been stuck on this problem for a few hours now and I'm kind of lost as to why it's not changing those values. Any help would be appreciated, thank you in advance!
Solution
Replace else if (deptID = 'T') with else if (deptID == 'T') and if (deptID = 'B') with if (deptID == 'B').
Explanation
The single equal sign = means assignment. Therefore, everytime the program runs, deptID will be assigned to B and the statement will return true, satisfying the if statement.
However, you want to compare two values to see if they are equal. Therefore, you must use == (equality).
Because the statements in the else if will never execute, TechTotal will remain uninitialised, and the value in that memory address just so happens to be 1.

Is there a way to hide variables in cout (C++)?

I'm having an issue figuring out how to hide a specific variable (if that is even possible) in a cout function. Basically I need to do a number guess game, which would be easy enough except our teacher wants us to do it like a random math equation. Which... would honestly still be fairly easy except the way we have to do it is the program has to randomly create the problem and then randomly pick one of the 3 numbers to display and the user has to guess the other two missing numbers. For example if the program picked 20 + 32 = 52 it could potentially display __ + 32 = __.
I've gotten that far however I can't figure out how to make it so it displays like that but still allows me to put the line something like this
cout << num1 //Hidden << " + " << num2 << " = " << num3 //Hidden
However like I said I don't even know if that is possible if not then I will probably have to rewrite the whole program. This is what I have so far:
int main()
{
int num1, num2, num3, random1, guess1, guess2;
string play = "";
cout << "Would you like to run the number guessing program? (enter yes or no): ";
getline(cin, play);
for (int i = 0; i < play.length(); i++)
{
play[i] = tolower(play[i]);
}
//Random seed
srand(time(0));
while (play == "yes")
{
//Generate random numbers and num3
num1 = 1 + rand() % 50 + 1;
num2 = 1 + rand() % 50 + 1;
num3 = num1 + num2;
int pickRandom[3] = { num1, num2, num3 };
//Display random elements
random1 = pickRandom[rand() % 3];
if (random1 == num1){
cout << "\nYour randomly generated number problem: " << num1 << " + " << "__" << " = " << "__" << endl;
}
if (random1 == num2){
cout << "\nYour randomly generated number problem: " << "__" << " + " << num2 << " = " << "__" << endl;
}
if (random1 == num3){
cout << "\nYour randomly generated number problem: " << "__" << " + " << "__" << " = " << num3 << endl;
}
//Get Guesses
cout << "\nBased off of this information please make an educated guess as to what the two missing numbers are.";
cout << "\n\nGuess for number 1 (between 1 and 100): ";
cin >> guess1;
while ((guess1 > 100) || (guess1 < 0))
{
cout << "\nSorry you need to enter an integer between 1 and 100" << endl;
cout << "\nGuess for number 1 (between 1 and 100): ";
cin >> guess1;
}
cout << "\n\nGuess for number 2 (between 1 and 100): ";
cin >> guess2;
while ((guess2 > 100) || (guess2 < 0))
{
cout << "\nSorry you need to enter an integer between 1 and 100" << endl;
cout << "\nGuess for number 2 (between 1 and 100: ";
cin >> guess2;
}
if (guess1 == )
}
return 0;
}
I don't think you can hide variables in cout. But you can use a variable instead of hardcoding "__".
For instance, you can simply write this:
if(guessed_var1_correctly)
var1 = num1
else
var1 = "__"
if(guessed_var2_correctly)
var2 = num2
else
var2 = "__"
if(guessed_var3_correctly)
var3 = num3
else
var3 = "__"
cout << "\nYour randomly generated number problem: " << var1 << " + " << var2 << " = " << var3" << endl;
where var1, var2, var3 are output variables. If the player guesses it correctly, it'll display the actual value num1, num2, or num3. If not, it'll simply display "__".

How to ask for user-input in nested loops?

I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.
There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.
Here's my code :
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
const int NumsToCal = 5;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
}
while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
Edit
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
int NumsToCal = 5;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
}
while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}

error expected unqualifed-id before '{' token [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int num1, num2, num3, num4, num5, result, result1, result2, result3, value, cont;
//number user enters/variable
int main()
{
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
;cin >> value
;cout << "please enter the first number number:";
cin >> num1
;cout << "please enter the second number: ";
cin >> num2
;if(value == '+' )
;result = num1 + num2;
cout << num1 << " plus " << num2 << " is equal to: " << result << ".\n";
if(value = '-' )
;result = num1 - num2;
cout << num1 << " minus " << num2 << " is equal to: " << result << ".\n";
if(value = '*' )
;result = num1 * num2;
cout << num1 << " times " << num2 << " is equal to: " << result << ".\n";
if(value = '/' )
;result = num1 / num2;
cout << num1 << " divided by " << num2 << " is equal to: " << result << ".\n";
}
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value1
cout << "please enter the next number:";
cin num3
else(cont = 0)
else(value1 = +)
;result1 = result + num3;
cout << result << " plus " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = -)
;result1 = result - num3
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = *)
;result1 = result * num3
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value1 = /)
;result1 = result / num3
cout << result << " divided by " << num3 << " is equal to: " << result1 << ".\n";
}
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "please enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value1
;cout << "please enter the next number: ";
cin >> num4
else(cont = 0)
else(value2 = +)
;result2 = result1 + num4;
cout << result1 << " plus " << num4 << " is equal to: " << result2 << ".\n";
else(value2 = -)
;result2 = result1 + num4;
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value2 = *)
;result2 = result1 * num4
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value2 = /)
;result2 = result1 / num4
cout << result << " divided by " << num3 << " is equal to: " << result1 << ".\n";
{
cout << "press 1 to enter more numbers, or press 0 to not"
cin >> cont
if(cont = 1)
cout << "enter the operation,+ for addtion,- for subtaction,* for multiplatcion,/ for division"
cin >> value2
;cout << "please enter the next number: ";
cin >> num5
if(value3 = +)
;result3 = result2 + num5;
cout << result2 << " plus " << num5 << " is equal to: " << result3 << ".\n";
else(value3 = -)
;result3 = result2 - num5
cout << result << " minus " << num3 << " is equal to: " << result1 << ".\n";
else(value3 = *)
;result3 = result2 * num5
cout << result << " times " << num3 << " is equal to: " << result1 << ".\n";
else(value3 = /)
;result3 = result2 / num5
return 0;
the error happens at line 34, where it says {
so please help me!
the code is for a basic calculator
feel free to use it if you can correct line 34!
I have no idea what is causeing it
I am a noob to the c++
codeing so please help!
I have done my own research and I cannot find it.
You can't say stuff like
else(value1 = +)
in C++. You must mean something else, but it is hard to guess what because there are so many errors in your code. In general, you cannot just type random characters and expect a functioning program.
The error stems from the additional { ... } blocks following the main function, since the compiler does not know what to do with code outside of declarations . But that is not the only problem with your code:
Putting a semicolon directly after an if statement means "if the condition is true do nothing anyway", and the next statement is executed either way.
if(value = +) should be if(value == '+') etc - you mixed the association = with comparison ==, plus you try using an operator + instead of a character '+'* what is else(something) supposed to do? Code blocks are put in {}s, not ()s
May I suggest you start programming in an easier language like Python? Its meaningful indentation and the lack of semicolons makes life a lot easier...
Get rid of all of the
}
{
and you'll be able to proceed to fixing your next error.
When you write braces like the following: }
you're closing your block of code, which in this case is your main function.
and when you write the following: {
The compiler thinks that you're trying to start a new function, but there's not function signature and you get an error.
It looks to me like you intend all of this code to be inside of your main function, so you want something like the following:
int main()
{
//insert all of your code here
return 0;
}