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.
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 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.
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 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
When I input a string that starts with the letter 'a' the program fails to check for that letter and excludes it. If i input a string that has an 'a' just not first in string, it checks out.
Here is the complete problem:
Write a program that will read a line of text and output a list of all the letters that occur in the text together with the number of times each letter occurs in the line. End the line with a period that serves as a sentinel value or delimiting character.
The letters should be listed in the following order: the most frequently occurring letter, the next most frequently occurring letter, and so forth.
Use two arrays, one to hold letters and one to hold integers. You may assume that the input uses all lowercase letters. For example, the input do be go bo. Should produce output similar to the following:
Letter Numbers of Occurrence
o 3
b 2
d 1
e 1
Note: you can modify the implementation of the selection sort algorithm in the book to sort the array in descending order. You can use either string type or c-string type in your program.
Code:
#include<iostream>
#include<string>
using namespace std;
void sort(char letters[],int letter_count[])
{
for(int i=0; i<26; i++)
{
int max = i;
for(int j=i; j<26; j++)
{
if(letter_count[j] > letter_count[max]) max = j;
}
int temp = letter_count[i];
letter_count[i] = letter_count[max];
letter_count[max] = temp;
char local = letters[i];
letters[i] = letters[max];
letters[max] = letters[i];
}
}
int main()
{
string str;
char letters[26];
int letter_count[26] = {0 };
cout <<"Enter a line of text :";
getline(cin,str);
for(int i=0; i<str.length(); i++)
letter_count[str[i]-'a']++;
for(int i=0; i<26; i++)
letters[i] = static_cast<char> ('a'+i);
sort(letters, letter_count);
cout <<"Letter Numbers of Occurrence" << endl;
for(int i=0; i<26; i++) {
if(letter_count[i]!=0)
cout << letters[i] << " " << letter_count[i]<<endl;
}
return 0;
}
Problem found on line 20, where you try to swap letters[i] with letters[max]. That line should be
letters[max] = local;
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 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
Enter an odd value greater than zero -- I understand how to do that.
Print out a triangle that looks like the following if 5 is entered:
54321
432
3
If 11 is entered:
10987654321
098765432
9876543
and so on
I see that we must divide the input by 10 and print the remainder but I'm having trouble printing the countdown.
for (i = n; i >= 1; i--)
i = i - 1;
use a for loop (start at 0), and on each iteration print a substring from i to string.length()-1 an use the set width to increase the indent, following code below:
string num = "10987654321";
for (int i = 0; i < num.length(); ++i){
cout << setw(i) << right << num.substr(i, num.length()-i) << endl;
}
This should give you your desired output. (if "<< right <<" doesn't work, swap "right" with "left")
ALSO
you need to include iomanip to use setw()
int num = 10987654321;
int numSpaces = 1;
cout << num << endl;
for (int i = 0; i >= 1; i--)
{
for (int j = 0; j < numSpaces; j++)
{
cout << " ";
}
cout << num % 10 << endl;
num = num / 10;
numSpaces++;
}
Voila a beautiful triangle :)