when I am trying to run this snippet of code I am getting some malfunctions I have not been able to pinpoint the cause of. The first two "cout" lines display the numbers 7 and 3, however, the last "cout" line displays numbers ranging from 50-60 usually (At the moment when I run it I get 55 and 51, which seems to somehow correlate a bit with the numbers I am trying to read). As far as I can tell from some googling and the books I have at hand this should be valid, but there's obviously something I am missing. Thank you for your time.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text = "73";
int one=0, two=0;
cout << text.at(0) << endl;
cout << text.at(1) << endl;
one = text.at(0);
two = text.at(1);
cout << one << endl << two << endl;
return 0;
}
the program functions correctly: text.at() returns a char, which you implicitly convert to an int. Then you print the value of that int, which are respectively 55 for "7" and 51 for "3" (look here).
Related
EDIT
I've seen a few wondering what I am asking for. I haven't written the algorithm for it yet, but after the program runs and outputs the 2 arrays, I need to output how many numbers matched. There is an example of that below. My apologies, I really thought that I made it as clear as possible. Here is the example again.
If the randomly generated array comes up with the numbers 0,1,2,3,4 in that order, and the user inputs into the "player" array the numbers 0,1,4,3,2 in that order, then they matched 2 digits (0 and 1). Basically it's not enough to just have the same numbers anywhere in the array, they must be in the same spot or index. I need to write an algorithm for this, though I have not yet. I'm sure I need a function with a for loop. I'm looking for any tips on how to go about it.
I've seen many iterations of this program on StackOverflow but none have been able to help me out so here I am asking for help.
I have this program that has 2 arrays. In one array that generates 5 random numbers from 0-9, the name is winningDigits. The other array (player) is for 5 numbers the user inputs. The goal of the user is to input 5 numbers and hope they all match with the randomly generated numbers.
I am almost done with the program, my only issue is trying to output how many digits were matched. For digits to match, they must be in the same spot in the player array as the winningDigits array.
For example
If the randomly generated array comes up with the numbers 0,1,2,3,4 in that order, and the user inputs into the "player" array the numbers 0,1,4,3,2 in that order, then they matched 2 digits (0 and 1). Basically it's not enough to just have the same numbers anywhere in the array, they must be in the same spot or index.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
int player[5];
int size = 5;
int count = 0;
cout << "Welcome to Lottery Simulator." << endl << endl;
cout << "Enter 5 numbers ranging from 0-9 one at a time, enter -1 when finished." << endl;
// Loop for user input
cout << "Enter number: ";
cin >> player[count];
while (player[count] != -1)
{
count++;
cout << "Enter number: ";
cin >> player[count];
}
cout << endl;
cout << "Winning Numbers User Numbers" << endl;
cout << "--------------- ------------" << endl;
// Generating random winning numbers
srand(time(0));
int winningDigits[size];
for (int i = 0; i < size; i++) {
winningDigits[i]= rand() % 10;
cout << setw(8) << winningDigits[i] << setw(17) << player[i] << endl;
}
return 0;
}
Now I'm thinking I'll need to make a function with a for loop, but so far I'm stuck. Just in case it wasn't clear enough, I am looking to output how many digits were matched. I hope my example from above will help you all understand what I'm looking for. Sorry, this is only my second post here and I am trying to make everything as clear as possible. Please ask if you need any information or help in understanding what I wrote.
I'm trying to examine a C++ code from my tutorial book. I've written this using CodeBlocks IDE:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
/*...*/
using namespace std;
/*...*/
int main (void){
cout << "Please enter name and age: \n\n:>";
string _sInput = "";
int _intInput = -1;
cin >> _sInput >> _intInput;
cout << "Hello " << _sInput << "!\n";
cout << "You have aged " << _intInput << " years.";
}
Based on what was discussed in the book by Mr. Stroustrup, now that I have given the variable _intInputan intial value, if I input wrong data like James Boy I am supposed to receive an output like this:
Hello James!
You have aged -1 years.
But what I get is You have aged 0 years. just like the time I have not given an intial value.
Is there anything wrong with my code or what?!
Since C++11, when reading integer or floating point number from istream fails, the target variable is set to 0. See this (cppreference.com) or this (stack overflow.com) for more on that.
This means, you can't use a sentinel value to detect parse error, you have to use for example fail() method to check if there was some error.
As per C++11, the value is set to zero, when an error occurs. See "If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value..." for more info.
I'm very new to coding (like one day ago), so you can pretty much assume I know nothing. I'm finding it very hard to debug my extremely simple programs and most of the help I've seen online is either over my head or too specific to the program.
This is the code I'm working on now.
/*
* LogBase8.cpp
*
* Created on: Feb 13, 2015
* Author: Holly
*/
//This program calculates the log base 8 of a number.
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
//Declare variables and constants.
float x, log_8_x;
int main()
{
//Prompt user to input number.
cout << "This program will calculate the log base 8 of a number. Input number to calculate" << endl;
cin >> x;
//Calculate log_8_x.
log_8_x = log(x) / log(8);
//Print log_8_x.
cout << "The log base 8 of " << x << " is " << log_8_x << endl;
//End main.
return (0);
}
I have an error in the log_8_x = log(x) / log(8); line that says
no match for 'operator=' (operand types are 'std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}' and 'double')
I'm using Eclipse, if that's relevant.
You have other errors as stated in your question, but anyway putting a \ to escape the newline appearing in the string literal here
cout << "This program will calculate the log base 8 of a number. Input \
fixes your compilation errors.
See fully working sample here please.
I've been tasked with creating a very simple random walk program for a C++ class. I wrote it and was fairly sure everything was correct but I am getting this strange bug. From 0 to 10 the steps line up even though the coordinates show that its taking alternating left and right steps.
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int steps,i,pos=10;
srand(13699);
cout << "Please enter the amount of steps to be taken: ";
cin >> steps;
cout << endl;
for (i=0;i<=steps;i++)
{
if (rand()%2)
pos+=1;
else
pos-=1;
cout << i << ": " << pos-10 << ": " << setw(pos) << "*" << endl;
}
} // main
There is very obviously some sort of pattern here, but for the life of me I can't figure it out... Here is a link to a screenshot of the output if it helps. http://i.stack.imgur.com/USx4U.png Thanks for any help y'all can give!
The answer is not in the code but rather in your interpretation of the output.
When the pos-10 is less than 0 then the area where you print this value is longer (because of the minus sign), then your 'walker' is shifted right a position in the output.
Similar reason when it goes from 9 to 10 it isn't right.
Think about what it means that the colons on the left are not in a straight line.
The "lining up" for i between 1 and 10 makes sense.
Take the first two lines, for example:
When i == 1, you have pos == 10, and the * is printed 10 spaces after the :.
When i == 2, you have pos == 9, and the * is printed 9 spaces after the :.
But because you are printing 0 (one character) in the first line and -1 (two characters) in the second line, the * end up on the same place in each line.
BTW, you are using the same value (13699) for seeding the RNG every time you run your program.
Try using a more "random" value, for example, a time-based one:
srand((unsigned int)time(NULL));
You'll need to #include <time.h> in your source file.
I have a C++ problem:
#include<iostream>
#include<stdio.h>
using namespace std;
int main() {
double k = 1.2366;
cout << printf("%.3lf\n", k);
}
the output is:
1.237
6
but I expect:
1.237
why do i get this additional 6 at the 2nd line ?
Well, you first call printf that prints out the number and then stream the result of printf (the number of characters printed) to cout.
This is because printf returns the number of printed characters. To get your code right, simply use std::cout directly with the desired precision:
std::cout << fixed << setprecision(3);
std::cout << k << "\n";
If you want to round results, read How do you round off decimal places in C++? which points you to e.g. floor/ceil/round. But take care to not round the value itself, if you use it again.