C++ Xcode lldb error: loops never work [closed] - c++

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 9 years ago.
Improve this question
thanks for taking the time to help me out.
I'm really new with C++ and Xcode. I was working on a simple program to help me understand loops, so my goal was to make a simple "echo machine". This is my code:
string words;
int main()
{
do {
cout << "Enter text.";
cin >> words;
cout << "You entetered " << words << "!";
}
while (words != "goodbye");
return 0;
}
My result is nothing but lldb in parenthesis. I am very frustrated and can't find what I'm doing wrong anywhere. Please help and thank you so much.

Are you just missing the include directives for the standard headers you're using?
Try this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string words;
do {
cout << "Enter text: ";
cin >> words;
cout << "You entered " << words << "!\n";
} while (words != "goodbye");
return 0;
}

Related

Compile error on Developer Command Prompt for VS 2019 using C++ [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 2 years ago.
Improve this question
The goal is to code a simple program that reads two strings and compare them to each other. The error (C2679 binary '>>') is pointing to the input line of the code, the 'cin' line. I've done the #include and the using declaration, but maybe I am missing a notation that isn't apparent to me:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
// define two strings
string s1, s2;
// read two strings
cin >> s1 >> s2 >> endl;
// compare two strings to determine if equal or report the larger
if(s1 != s2){
if(word1 > word2)
cout << s1 << " is larger than " << s2 << endl;
else
cout << s2 << " is larger than " << s1 << endl;
} else
cout << "The words are equal in size" << endl;
return 0;
}
Thanks in advance!
Using endl with cin doesn't make sense. >> endl should be removed.
word1 and word2 are not declared here. It seems if(word1 > word2) should be if(s1 > s2).

Program stops without running the while loop [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
My while loop is not working. The code runs correctly on the Codecademy website compiler. I then compile it with Visual Studio, run it from the Command prompt and input a number. The program stops prematurely even if the number is the correct one.
#include <cstdlib>
#include <iostream>
int main() {
int answer = 8;
int guess;
int tries;
std::cout << "I have a number between 1-10.\n";
std::cout << "Please guess it: ";
std::cin >> guess;
while (guess != 8 && tries < 50) {
std::cout << "Wrong guess, try again: ";
std::cin >> guess;
tries++;
}
if (guess == 8) {
std::cout << "You got it!\n";
}
}
As #rsjaffe and #Ken White have said in the comments, the tries variable is unitiailized, meaning that the location in memory that the variable is pointing to is "junk" (left over memory). Try to give it an initial value, like this:
int tries = 0;
which will instantiate and initialize the tries variable.

C++ Can't use vector and string literal in cout [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
I'm a student who just learned to use C++ for a few weeks. I write C++ code on Code:: Block and I am trying to run this code to learn about vector:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int> intVector;
for(int i = 0; i < 10; i++)
{
intVector.push_back(i+1);
}
cout << "Numbers in vector: ";
for(int i = 0; i < intVector.size(); i++)
{
cout << intVector[i] + " ";
}
}
But the output is really weird:
Numbers in vector: vector::_M_emplace_back_auxector::_M_emplace_back_auxctor::_M_emplace_back_auxtor::_M_emplace_back_auxor::_M_emplace_back_auxr::_M_emplace_back_aux::_M_emplace_back_aux:_M_emplace_back_aux_M_emplace_back_aux
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.
Does anyone know how to fix this problem? Do I use vector in the wrong way?
Try changing the cout line like this:
cout << intVector[i] << " ";
What you're trying to do is add an int& and a string literal -- in some compilers it will emit a warning indicating for you that you're not getting what you intended for.
For example with Clang:
warning: adding '__gnu_cxx::__alloc_traits>::value_type' (aka 'int') to a string does not append to the string [-Wstring-plus-int]
cout << intVector[i] + " ";
~~~~~~~~~~~~~^~~~~

why am I getting random results when incrementing an int [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 8 years ago.
Improve this question
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.

Can't Print String Array Element [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 8 years ago.
Improve this question
Whenever I try to run this program it returns an error saying:
no operator "<<" matches these operands
Also note that the program only runs into this problem in the getChoice() function.
#include <iostream>
#include "utilities.h"
using namespace std;
int getChoice(string inChoices[]){
int numOfChoices = sizeof(inChoices) / sizeof(inChoices[0]);
string x = inChoices[0];
string y = inChoices[1];
cout << x << endl << y << endl;
return numOfChoices;
}
int main()
{
string choices[2] = { "Happy Day", "Even Better Day" };
cout << utilities::getChoice(choices) << endl;
cout << endl << sizeof(choices) / sizeof(choices[0]) << endl;
}
You need also to include the string header:
#include <string>
You need to #include <string>
And your calculation of numOfChoices in getChoice() is wrong, since the parameter inChoices is actually a "pointer to string" instead of "array of strings".