string size limit input cin.get() and getline() - c++11

In this project the user can type in a text(maximum 140 characters).
so for this limitation I once used getline():
string text;
getline(cin, text);
text = text.substr(1, 140);
but in this case the result of cout << text << endl; is an empty string.
so I used cin.get() like:
cin.get(text, 140);
this time I get this error: no matching function for call to ‘std::basic_istream::get(std::__cxx11::string&, int)’
note that I have included <iostream>
so the question is how can I fix this why is this happening?

Your first approach is sound with one correction - you need to use
text = text.substr(0, 140);
instead of text = text.substr(1, 140);. Containers (which includes a string) in C/C++ start with index 0 and you are requesting the string to be trimmed from position 1. This is perfectly fine, but if the string happens to be only one character long, calling text.substr(1, 140); will not necessarily cause the program to crash, but will not end up in the desired output either.
According to this source, substr will throw an out of range exception if called with starting position larger than string length. In case of a one character string, position 1 would be equal to string length, but the return value is not meaningful (in fact, it may even be an undefined behavior but I cannot find a confirmation of this statement - in yours and my case, calling it returns an empty string). I recommend you test it yourself in the interactive coding section following the link above.
Your second approach tried to pass a string to a function that expected C-style character arrays. Again, more can be found here. Like the error said, the compiler couldn't find a matching function because the argument was a string and not the char array. Some functions will perform a conversion of string to char, but this is not the case here. You could convert the string to char array yourself, as for instance described in this post, but the first approach is much more in line with C++ practices.
Last note - currently you're only reading a single line of input, I assume you will want to change that.

Related

The code always outputs "not"

The following code always outputs "not":
print "input a number please. "
TestNumber = gets
if TestNumber % 2 == 0
print "The number is even"
else
print "The number is not even"
end
What is going wrong with my code?
The gets() method returns an object of type String.
When you call %() on a String object, the return value is a new String object (usually it changes the text. You can read more about string formatting here).
Since there are no String objects that == 0, the if/else will always take the same path.
If you want to use the return value of gets() like a number, you will need to transform it into one first. The simplest approach is probably to use the to_i() method on String objects, which returns a new 'Integer' object. If you're doing something where the user input will not always be an integer (e.g. 3.14 or 1.5), you might need to use a different approach.
One last thing: in your example the result of gets() is saved into a constant called TestNumber. Constants are different to normal variables, and they will probably cause problems if you're not using them intentionally. Normal variables don't start with capital letters. (You can read more about ruby variables here). In ruby you need to write you variable names like this: test_number.
I suspect your Testnumber variable might be interpreted as a string during the operation. make sure the testnum is converted to an integer first even if you put in say 100 it could be its being interpreted as the stirng "100" and not the integer 100.
A similar issue can be found here: Ruby Modulo Division
You have to convert TestNumber from string to integer, as your input has linefeed and/or other unwanted characters that do not match an integer.
Use TestNumber = gets.to_i to convert to integer before testing.

In TI-BASIC, how do I add a variable in the middle of a String?

I am wondering how to make something where if X=5 and Y=2, then have it output something like
Hello 2 World 5.
In Java I would do
String a = "Hello " + Y + " World " + X;
System.out.println(a);
So how would I do that in TI-BASIC?
You have two issues to work out, concatenating strings and converting integers to a string representation.
String concatenation is very straightforward and utilizes the + operator. In your example:
"Hello " + "World"
Will yield the string "Hello World'.
Converting numbers to strings is not as easy in TI-BASIC, but a method for doing so compatible with the TI-83+/84+ series is available here. The following code and explanation are quoted from the linked page:
:"?
:For(X,1,1+log(N
:sub("0123456789",ipart(10fpart(N10^(-X)))+1,1)+Ans
:End
:sub(Ans,1,length(Ans)-1?Str1
With our number stored in N, we loop through each digit of N and store
the numeric character to our string that is at the matching position
in our substring. You access the individual digit in the number by
using iPart(10fPart(A/10^(X, and then locate where it is in the string
"0123456789". The reason you need to add 1 is so that it works with
the 0 digit.
In order to construct a string with all of the digits of the number, we first create a dummy string. This is what the "? is used
for. Each time through the For( loop, we concatenate the string from
before (which is still stored in the Ans variable) to the next numeric
character that is found in N. Using Ans allows us to not have to use
another string variable, since Ans can act like a string and it gets
updated accordingly, and Ans is also faster than a string variable.
By the time we are done with the For( loop, all of our numeric characters are put together in Ans. However, because we stored a dummy
character to the string initially, we now need to remove it, which we
do by getting the substring from the first character to the second to
last character of the string. Finally, we store the string to a more
permanent variable (in this case, Str1) for future use.
Once converted to a string, you can simply use the + operator to concatenate your string literals with the converted number strings.
You should also take a look at a similar Stack Overflow question which addresses a similar issue.
For this issue you can use the toString( function which was introduced in version 5.2.0. This function translates a number to a string which you can use to display numbers and strings together easily. It would end up like this:
Disp "Hello "+toString(Y)+" World "+toString(X)
If you know the length of "Hello" and "World," then you can simply use Output() because Disp creates a new line after every statement.

What is meant by returning half of a supplementary character?

When you use .charAt method to convert a particular character in a string of string class to a character of char type, a code unit.
For example:
String greeting = "Hello";
Char character = greeting.charAt(0);
Character of char type is set to 'H'.
But you use a single index number for a supplementary character, only the first or second half is returned.
So for example, invoke the same method on $\mathcal {P} (\mathbb{Z})$ (set of integer), only the first half is returned.
All these are what is said in the book. But what does it meant by first half? First half of the two unit?
The language is java
Let me clarify:
(The latex code for set of integer seems to not be working)
String greeting = "(set of integer) is important";
char ch = greeting.charAt(0)
Index zero refers to the symbol for set of integer in math but b/c it is a supplementary(encoded by "\uD835\uDD6B"). Only the first half of this character is returned and stored in ch.
But what is first half?

Cannot cast object '(10)' with class 'java.lang.String' to class 'java.lang.Integer'

I am using ireport 3.7.1. I have made a connection with my database.I have a procedure which when given an input in number ,it returns the word format of the number i.e if I give input 10,it will return ten. The problem is when I am executing the procedure in pl/sql developer,I am getting the proper output but when I am firing the same procedure in ireport it's giving me this exception
Cannot cast object '(10)' with class 'java.lang.String' to class 'java.lang.Integer' .
Casting straight from a String to an Integer is not possible. You'll want to use the function Integer.parseInt(stringNumber);
(10) isn't a properly formated integer. Not even for PL/SQL:
select '(10)' +0 from dual;
> ORA-01722: invalid number
I could only suggest you to trace back the point where those ( ) come from. And fix your code at that position instead. Just a wild guess, some number formats use parenthesis to represent negative numbers. Maybe this is your case?
That being said, if you still want to locally remove the parenthesis that have somehow lurked inside of your string:
String str = "(10)";
int value = Integer.parseInt(str.substring(1, str.length()-1));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// *blindly* get away of first and last character
// assuming those are `(` and `)`
For something a little bit more robust, and assuming parenthesis denotes negative numbers, you should try some regex:
String str = "(10)";
str = str.replaceFirst("\\(([0-9]+)\\)", "-$1");
// ^^^ ^^^ ^
// replace integer between parenthesis by its negative value
// i.e.: "(10)" become "-10" (as a *string*)
int value = Integer.parseInt(str);

c - Reading individual characters from a char

I am working on a project using DirectSound and am trying to make it so that the header information for the sound file is passed to the processing method. WAV files have a RIFF chunkID at the start. In a call to the ProcessWaveFile method, I pass the filepath as well as the header information. The 'RIFF' chunkID is stored in a char. I need to be able to check individual characters of the chunkID against the actual file chunkID to ensure the file is correct. This is a snippet of my code:
if((waveFileHeader.chunkId[0] != chunkID[0]) || (waveFileHeader.chunkId[1] != chunkID[1]) ||
(waveFileHeader.chunkId[2] != chunkID[2]) || (waveFileHeader.chunkId[3] != chunkID[3]))
{
MessageBox(hwnd, L"ChunkID not in the right Format.", L"Error", MB_OK);
return false;
}
chunkId is the file's actual ID whereas chunkID is the ID passed through the function to check. As you can see I'm trying to handle it like an array here. The chunkId is stored in a char[]. Should I store the chunkID in an array too? How would I specify it?
bool ProcessWaveFile(char*, IDirectSoundBuffer8**, HWND, int, char, char, char, char, std::string, int, int);
Above is the header file line for the ProcessWaveFile method. The chunkID is specified as one of the chars. I could change it to char[] but the difficulty comes when actually calling the method. Here is an example call:
result1 = ProcessWaveFile("../terrain_sky/data/sound01.wav", &m_secondaryBuffer1, hwnd,
44100, 'RIFF', 'fmt ', 'WAVE', 'data', "WAVE_PCM_FORMAT", 16, 2);
How could I declare the array values containg RIFF in this call without disrupting the chain of variables?
If you are looking for a convenient way to check that the file starts with 'RIFF', there may be a more convenient way for you to find out. Since the ChunkID is 4 bytes long, you may want to store what you expect 'RIFF' in a datatype that is also 4 bytes long (instead of a char array of length 4), then parse the first four bytes as, for example, an int.
I have a wav file that starts with RIFF here, here's what number you get when you interpret the first four bytes as an int.
od -ci -N4 ./test.wav
0000000 R I F F
1179011410
So the option -ci first prints each byte as its ASCII equivalent (RIFF), and then prints each set of 4 bytes as an integer. So really, 1179011410 = 'RIFF' and the integer may be easier for you to check for.
But as an aside, I'm not really sure why you need to pass 'RIFF' into the ProcessWaveFile() function. If you only ever deal with RIFF files, you might get away with having that information stored in the ProcessWaveFile() function itself.

Resources