Scanf c++ on a string [closed] - c++

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 8 years ago.
Improve this question
I have been looking and I have not found anything on scanf that really helps me. I am completly new to this and was hoping for help on reading a string with scanf. The first thre numbers can be any digits. I was attempting to read them into a variable int. the last one in the string is a char. this is my string
(1,2,123, 0)
(1,2,1,s)
This is my code:
int i,j,k;
char c, final;
scanf ("%c", c, "%d",&i, "%c", c, "%d", &j, "%d",&k, "%c", final);
I know this is not right but any help is appreciated

If the first 3 are digits and the last one is a character and are seperated by a space,which you want to assign to 3 integer variables and a character variable, use scanf like this:
int a,b,c;
char ch;
scanf ("%d%d%d %c",&a,&b,&c,&ch);
Or else if you want to extract 3 integers and a character from a string , use sscanf. It is not possible to do it with scanf.

Related

How to randomly display an user input string using c++? [closed]

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 3 years ago.
Improve this question
I am trying to randomly display a user input string using c++ but I couldn't find a way to do.
Currently I am pre defining some strings
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
const string wordlist[4] = {"hi","hello","what's up","wassup"};
string word = wordlist [rand()%4];
cout<<word;
return 0;
}
What I want is:- I don't want to pre define them. I want the user to type in 4 words and I will display a word from the 4 words given by the user (randomly).
To do that you will have to first remove const qualifier from wordlist array.
srand(time(0));
std::vector<string> wordlist(4);
for(auto& s: wordlist) std::cin>>s;
string word = wordlist [rand()%4];
cout<<word;
Line 3 is C++11's range based for loop, this way I can easily loop over elements of std::vector<string> without indices.
If there are multiple words in a string then use getline(cin,s) accordingly. Then input each string in a new line. But be careful when mixing cin and getline for taking input.
You can use std::array if the size is going to be fixed (i.e. 4) as mentioned in comments.

I can't get an algorithm based on reading a character array to work [closed]

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 4 years ago.
Improve this question
I need to turn the inputted character array into all lowercases. The reading stops at the '.' character. I have to do it this way, without using the string variable nor any function inside the library.
#include <iostream>
using namespace std;
int main(){
char s[200], *p=s;
cin.getline(s, 200, '.');
while(p){
if('a' <= *p && *p <= 'z') *p += 'A'-'a';
p++;
}
cout << s;
}
The algorithm is supposed to check every character in the array until it meets a null pointer. For every character, it should then check if it is a lowercase character. If it is, it changes it into an uppercase letter (by decreasing 'a' it memorises the difference between the first letter of the alphabet and the letter it is referring to, by increasing 'A' it adds that difference to the first letter of the uppercase alphabet, so to speak, thus obtaining the uppercase version of the letter).
I've no clue where my mistake is, but my compiler crashes when I try to run it.
while (p)
should be
while (*p)
If you're looking to stop on the null terminator, you need to dereference the pointer or you'll test that the pointer isn't null instead.
Sidenote: You may find the std::tolower function helpful.

Cstring input using get() c++ [closed]

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 8 years ago.
Improve this question
Trying to write the following function but confused, as get() only reads in the first character?
Write C-string's chars to the screen one char at a time.
void writeString(const char*)
Rule:
cannot use [].
Hints:use put();
make use of '\0' – but don't write it out.
It sounds like you just need a simple loop to output the string. Something like this perhaps.
void writeString(const char* str)
{
while(str++ != '\0') put(*str);
}
The while(str++ != '\0') will iterate over the string buffer pointed to by str and output each character. It also increments the str pointer to the next character and checks for null terminator ('\0').

How to convert an ASCII int to string? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
recently I made a program that, given a string, the function returns its corresponding ascii code. For example, the string "h", the function returns an int 104. Now, I want to do the reverse process, ie, given an int, return its corresponding ascii character. For example, given the 104 int return the string "h". Please, help.
Looking at the string constructors, we can see one that takes a count and a character value. So we can use that:
return std::string(1, ascii_value);
You probably don't need a whole string for this. Given that you're looking for a single character, the correct type to use is a char:
int x = 65;
char xc = (char)x;
assert(xc == 'A');
char c;
...
std::string mystring(1, c);

Where is the serialization going wrong? [closed]

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 8 years ago.
Improve this question
void insert_into_stream(std::ostream& stream, int number)
{
int length = sizeof(int);
char insert_buffer[sizeof(int)];
memcpy(insert_buffer, &number, length);
stream.write(insert_buffer, length);
}
int int_from_string(std::string string)
{
int a;
std::istringstream(string)>>a;
return a;
}
This code used to work before, I don't remember what slight change I did and it isn't working anymore.
When a number (for example, 8001) comes in, and I'm debugging just before the last statement, insert_buffer contains 'A', so obviously something is going wrong and the 2nd function doesn't retrieve 8001.
Note - I do convert the stream to a string before sending it to the 2nd function.
Where is the first function wrong?
---edit----
Yes, I was wrong, the first function is actually doing exactly what it should, the second is wrong, can anyone please fix that?
These two functions are doing completely different things.
The first function is writing out the raw binary representation of an integer to a stream.
You have just copied the bits, this is the correct thing to do if you are serialising out to a binary file.
To convert it back, you would have to read in those 4 bytes, and cast that into an integer, just like you're doing the other way round.
Of course when you examine the characters they're going to be a one byte ascii representation of the integer bits. So 'A' is a perfectly reasonable thing to be there, as is anything else as it is entirely meaningless.
The second function however, is doing an ASCII Number to Integer number conversion. i.e. atoi. This is meaningless for what you're trying to do as the characters aren't ascii numbers, they're binary integer numbers.
Edit for edit: You want something like this, just the opposite of what you did above.
int int_from_string(const char* number)
{
int a;
memcpy(&a, number, sizeof(int));
return a;
}
Try to use
stream.write((const char*)&number, sizeof(number));
It's much shorter and you can also change the type of number and it will work (For simple types).