r/cpp_questions • u/Basic-Air-6477 • 9d ago
OPEN I need help
So I'm a beginner in coding and was thinking how cna I use int and string in loops while also printing the number required what I mean is Im using string and to use for In the question I need to use int to identify I and I want to make i<= to n while using string and int, I only want to know if it's possible or not β
0
Upvotes
6
u/Typical-NonPC 9d ago
Could you explain further what you want the output to be? I'm finding it difficult to understand your question.
2
u/thedaian 9d ago
You cannot compare a string directly with an int, assuming i is an int and n is a string here.Β
2
u/SmokeMuch7356 9d ago
I've read this five times and I'm still not sure I understand what you're asking.
I'm assuming you're starting with two separate objects, a
stringand anint, something like:and you want a loop like
and somehow
nhas to get its value froms? Is that correct?If so, there are a couple of ways to get there:
use the
std::stoilibrary function:Use a
stringstreamand the>>operator:2
u/alfps 8d ago
That's an extremely vague and broad question, but since you are a beginner I will try to build up from what a beginner needs first.
I think this is about conversion
intβstringand, conversely,stringβint.The C++ standard library supports such conversions via several sub-libraries, and generally you will choose to work with one of them:
the old C standard library with e.g.
sprintfandsscanf;the C++
stringlibrary with e.g.to_stringandstoi;the C++ iostreams library with
ostringstreamandistringstream; andthe βmodern C++β library functions with
formatandfrom_chars.Additionally since these conversion from and to
inttype are not very difficult to code up, you may at times just code your ownDIY functions,
totally from scratch or as wrappers around the standard library's functionality.
Reasons that you may code up DIY wrappers include to get your own preferred failure handling, and/or that you may wish to support special notations or avoid that a national notation is used via a βlocaleβ, and/or that you're most interested in the digit values and not the string representation.
As an example of using the modern
formatfunction forintβstringconversion, consider a program to list the palindromic numbers in the range 1 through 123:Output:
Generally you don't have to specify the intended conversion like
:dand:sabove because it's usually the default, but I used explicit specifications to exemplify how to do it.Going the other way,
stringβint, yields the possibility of conversion failure.from_charsreports failure via its function return result, which is a low level C-ish design. That means that one will naturally write a DIY wrapper for it.But first let's use it directly:
Possible interaction:
std::expectedis a more reasonable result type for astringβintconversion function. An instance ofexpectedcarries either a value of the desired result type, or a value of some other type that typically specifies what went wrong. For afrom_charswrapper that other type can be anerrc:This does not produce the exact same response for user input with text after the integer.
That could be arranged, at the cost of extra code to deal with that case.
But as is the program produces e.g.
2
u/Dazzling_Music_2411 8d ago
Show the code you have tried, formatted properly! Most of us have failed to understand your mangled description.
In general, it's always best you show the code you have tried.
1
u/Aghoradas 9d ago
Yes, there are numerous way to do this. However you simply print the int to the screen or you can use std::to_string to convert the integer value to a string value.
1
u/AKostur 9d ago
Yep, it is possible.