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 4 years ago.
Improve this question
This code should ask for in put with "What is If (selection)?"
and this part it does. but once you input the answer as "Provides the ability to either do a block of code or skip that block of code." the out come should be "Correct!" but instead it either asks to press key to end or re asks the question. does anyone have and advice as to how i could fix this?
srand((unsigned)time(0));
int random_interger;
int lowest = 2, highest = 18;
int range = (highest - lowest) + 1;
for (int index = 0; index < 20; index++) {
random_interger = lowest + int(range*rand() / (RAND_MAX + 1.0));
if (random_interger == IF) {
cout << " What is If (selection)?" << endl;
cin >> IFs;
if (IFs == "Provides the ability to either do a block of code or
skip that block of code.") {
cout << "Correct!" << endl;
}
string IFs;
cin >> IFs;
Even if the user types exactly the long line you expect (he won't), this wouldn't work. cin >> into a string will read just one word. You would get only "Provides".
Look up the getline API. But even then, I doubt anyone would type those lines exactly.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I'm new to programming and i have problem with some items
i would appreciate any help
first i started initializing the vector as followed but i couldn't end the loop with Ctrl+Z
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector <double> temps;
double temp;
cout << "Enter a sequence of tempreatures : " << "\n" ;
while (cin >> temp){
temps.push_back(temp);
}
double sum = 0;
for (int i = 0; i< temps.size(); ++i)
sum += temps[i];
cout << "Mean temprature : " << sum / temps.size() << "\n";
sort(temps.begin(), temps.end());
cout << "Median temprature : " << temps[temps.size() / 2];
then i changed the while into this format :
cout << "ENter a sequence of tempreatures ending in 1500 : " << "\n" ;
while (cin >> temp){
if (temp == 1500)
break;
temps.push_back(temp);
}
now i have this error
"vector subscript out of range"
apparently break does not work properly here
what should i do?
Your issue is in the check condition of for loop.
for (int i = 0; i, temps.size(); ++i)
sum += temps[i];
It should be
for (int i = 0; i < temps.size(); ++i)
i, temps.size() will evaluate and then ignore the part before , and are left with temps.size() as check condition which will always be greater than 0 if you push_back at least one element and your loop will never end.You might want to read how ,(comma) works.
If you switch to std::getline into a string instead of std::cin into a double, you can check whether the input is empty:
std::string input;
std::getline(std::cin, input);
while (!input.empty()){
temps.push_back(atof(input.c_str()));
std::getline(std::cin, input);
}
If you also fix the for-loop as mentioned by Gaurav Sehgal, it works fine (Enter all numbers then hit enter without any input).
If you are on windows then you have to do
CTRL + Z
If you are on Unix based(Linux/Mac) then you have to do
CTRL + D
This will give the end of file signal and you will be able to break the loop
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 5 years ago.
Improve this question
I am struggling to get this program to work. Could someone please look it over to see what is wrong? (The compiler says that sum_positive is not defined in this way.) Also, I have searched this problem and found an answer on here, but I did not see the problem that way and want to avoid plagiarism when I turn in my homework. Thank you!
Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately.
Program:
#include <iostream>
using namespace std;
int main()
{
int number, n = 10, sum_positive = 0, sum_negative = 0, sum = 0;
int count = 0, positive_count = 0, negative_count = 0;
cout << "Please enter in 10 whole numbers, one at a time." << endl;
do
{
cout << "Please enter another number." << endl;
cin >> number;
n--;
}
while (n > 0);
if (number >= 0)
{
sum_positive += number;
//positive_count++; count++;
}
else
{
sum_negative += number;
//negative_count++; count++;
}
sum = sum_positive + sum_negative;
cout << sum_postive << endl;
cout << sum_negative << endl;
cout << sum << endl;
return 0;
}
there is a spelling mistake in your code of sum_positive in line (cout << sum_postive << endl;) - you have written sum_postive but you declared sum_positive.
you are running a loop and taking input in the same variable number, so it will overwrite all the inputs and will store only the last value in variable number which user entered. So your sum will always be equal to the last value user entered.
For this you need to use an array.
Example:
int number[10], n=10, sum_positive=0;
do{
cout<<"e`enter code here`nter number:";
cin>>number[n];
n--;
}while(n>0);
now for sum also you need to use an loop.
If you do not know about an array, study about it how to use it.
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 came across a question whereby you are told there will be unspecified number of queries hence you got to keep on taking input for this unspecified number of queries
all i know is that in c++ or even in another programming language, when the program needs to take unspecified number of input, we prompt the user to enter a certain value which will be used to terminate the infinite loop e.g
for (;;)
{
cout<<"enter 0 to stop taking input"<<endl;
int value;
cin>>value;
if (value==0)
{break;}
}
my question is how will i handle the question stating the input will be unspecified and its in an online environment
for this kind of problem you will use a loop control variable and a while loop for user input validation
while loop is use for input validation while for loop is use for specific
number of times only and it is not really suggested for unknown number of times
same with while loop, do while loop can also perform input validation the
only difference is, it will prompt the user first before evaluating the test condition
int myNum; // this is the loop control variable
cout << "enter a number, enter 0 to stop taking input " << endl;
cin >> myNum;
while(myNum!=0)
{
cout << "cograts you did not enter zero digit" << endl;
cin >> myNum;
}
use while (cin >> a) :
to see how if it works create an input file 1.in and write a bunch of numbers in it, then pipe it to your executable fle ./a.out <1.in
#include <iostream>
using namespace std;
int main() {
int a;
while (cin >> a) {
cout << a << "\n";
}
return 0;
}
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 am learning C++ and I have a problem with my program. It should print out following if n=11:
*---------*
-*-------*-
--*-----*--
---*---*---
----*-*----
-----*-----
----*-*----
---*---*---
--*-----*--
-*-------*-
*---------*
This is my code, which works correctly with n=5, but not with greater numbers:
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter size (n x n): " << endl;
cin >> n;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (i%n==j%n) cout << '*';
else if (i%(n-i)==j%(n-j)) cout << '*';
else cout << '-';
}
cout << endl;
}
return 0;
}
This is being printed out if n=11:
*---------*
-*----*--*-
--*-----*--
---*---*---
----*------
-----*-----
-*----*--*-
---*---*---
--*-----*--
-*----*--*-
*---------*
I see that I have successfully wrote how to print out one of '*' diagnoles. But something isn't working with other one, which is going backwards.
Unfortunately, I am not being able to resolve this problem and need your advice. What am I doing wrong? How to debug such problems?
This problem is really simple to debug.
Take a look at the first erroneous *. It appears at the position with i=1, j=6. With n=11, your condition i%(n-i)==j%(n-j) becomes 1%(11-1) == 6%(11-6) which is effectively true because the expression evaluates to 1 on both sides.
What is behind this expression? Why do you use this kind of if to determine whether the cell belongs to the second diagonal? Try to write down each pair i, j which should be printed on the second diagonal, and you should notice a more simple pattern.
P.S. In the expression if (i%n==j%n) you don't have to take operands modulo n, because both of them are less than n, so it is redundant and may be rewritten simply as if (i == j).
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 7 years ago.
Improve this question
I am trying to make a part of the game hangman, where the user inputs a letter and then a loop check for that letter in a random array. If the letter is found, it then couts a changed array including now that letter and offers the user to again, input another letter. It seems for loops are not working since the program doesnt scan the whole array for ever letter inputted. How can I fix this?
int main(){
string guess[25];
string password[5];
srand((unsigned)time(0));
string letters[5] = {"_ ","_ ","_ ","_ ","_ "};
char array[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','z'};
for(int r = 0; r < 5; r++){
int g = rand() % 24;
password[r] = array[g];
}
cout << endl;
for(int z = 0; z < 25; z++){
cout << "Enter Letter: " << endl;
cin >> guess[z];
for(int b = 0; b < 5; b++){
if(uguess[z] == password[b]){
letters[b] = guess[b];
cout << letters[b];
}else{
cout << letters[b];
}
}
cout << endl;
}
can someone point me in the right direction. Thanks
It always says that the word being guessed if asdfg, but it messes it up very badly, as in doesn't always show the letter, even if it has been guessed, it shows it later.
crke[b] = ugib[b];
This line should be:
crke[b] = ugib[z];
You might want to consider investing some time in learning how to use a debugger, which would've helped you figure it out.