Loop to keep adding spaces in string? - visual-c++

I have the following code:
sHexPic = string_to_hex(sPic);
sHexPic.insert(sHexPic.begin() + 2,' ');
sHexPic.insert(2," ");
I would like to know how I can put this into a counted loop and add a space after every 2nd character. So far all this does is make this string "35498700" into "35 498700", which in the end I want the final result to be something like "35 49 87 00".
I assume you would have to get the length of the string and the amount of characters in it.
I am trying to achieve this in c++/cli.
Thanks.

Here's how it would be done in C++, using a string :) (I'm using C libraries cuz I'm more familiar with C)
#include <stdio.h>
#include <string>
using namespace std;
int main()
(
string X;
int i;
int y;
X = 35498700;
y= X.size();
for(i=2;i<y;i+=2)
{
X.insert(i," ");
y=x.size(); //To update size of x
i++; //To skip the inserted space
}
printf("%s",X);
return 0;
}
Have fun :)
That would "probably" work. If it didn't then please mention so :)

Related

I'm need to count every word, line , and character of a given string

as you can see i'm trying to get the word count, character count and line count but the below code is not working.
#include<iostream>
using namespace std;
int main()
{
char ch;
int wc=1, lc=1, cc=0;
while((ch=cin.get())!='*')
{
cc++;
if(ch==' ')
{
wc++;
}
else if(ch=='\n')
{
wc++;
lc++;
}
}
cout<<"\n the number of character=="<<cc;
cout<<"\n the number of words=="<<wc;
cout<<"\n the number of lines=="<<lc;
return 0;
}
I entered your code and compiled it with g++. It is working without any problems. Can you post the error you get or did it compile ?
Maybe your visual c++ compiler is not working right. The code itself should work.
Edit: Below a different version of the above code, where no text input is threaded as zero words and EOF is also a break condition of the loop.
EOF depends on your system, on Windows it is Control + z, on Linux it might be Control + d.
The input text might have multiple spaces between words. Punctuation characters and digits (0-9) are threaded as word delimiters as good as possible. Underscore, backticks, tildes and apostrophe like in "don't" are handled as part of a word.
Curly brackets are handled as part of a word to keep the code simple but normal brackets are delimiters.
#include <iostream>
using namespace std;
int main()
{
int ch, wc=0, lc=1, cc=0, old=0;
cout<<"Enter your text, exit with '*':\n";
while ((ch=cin.get())!='*' && ch!=EOF)
{
cc++;
if (old<='?' && old!='\'')
wc += !(ch<='?' && ch!='\'');
lc += ((old=ch)=='\n');
}
cout<<"\nthe number of character=="<<cc
<<"\nthe number of words=="<<wc
<<"\nthe number of lines=="<<lc<<"\n";
return 0;
}

How to store Integers from a string without using the getline function. C++

Sorry to ask, but I been looking everywhere to find a way to extract the integers from this set of strings:
{(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}
I don't really need the homework done, if you could link me to an example, I'll appreciate it.
thank you in advanced.
If you just want to access the integers from a line like that, one way is to simply continue reading integers while you can.
If, for some reason, you find an integer read failing (because there's a { in the input stream, for example), just skip over that single character and keep going.
Sample code for this is:
#include <iostream>
int main() {
int intVal; // for getting int
char charVal; // for skipping chars
while (true) {
while (! (std::cin >> intVal)) { // while no integer available
std::cin.clear(); // clear fail bit and
if (! (std::cin >> charVal)) { // skip the offending char.
return 0; // if no char left, end of file.
}
}
std::cout << intVal << '\n'; // print int and carry on
}
return 0;
}
A transcript follows:
pax> echo '{(314159,271828),(42,-1)}' | ./testprog
314159
271828
42
-1

using malloc in dgels function of lapacke

i am trying to use dgels function of lapacke:
when i use it with malloc fucntion. it doesnot give correct value.
can anybody tell me please what is the mistake when i use malloc and create a matrix?
thankyou
/* Calling DGELS using row-major order */
#include <stdio.h>
#include <lapacke.h>
#include <conio.h>
#include <malloc.h>
int main ()
{
double a[3][2] = {{1,0},{1,1},{1,2}};
double **outputArray;
int designs=3;
int i,j,d,i_mal;
lapack_int info,m,n,lda,ldb,nrhs;
double outputArray[3][1] = {{6},{0},{0}};*/
outputArray = (double**) malloc(3* sizeof(double*));
for(i_mal=0;i_mal<3;i_mal++)
{
outputArray[i_mal] = (double*) malloc(1* sizeof(double));
}
for (i=0;i<designs;i++)
{
printf("put first value");
scanf("%lf",&outputArray[i][0]);
}
m = 3;
n = 2;
nrhs = 1;
lda = 2;
ldb = 1;
info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*outputArray,ldb);
for(i=0;i<m;i++)
{
for(j=0;j<nrhs;j++)
{
printf("%lf ",outputArray[i][j]);
}
printf("\n");
}
getch();
return (info);
}
The problem may come from outputArray not being contiguous in memory. You may use something like this instead :
outputArray = (double**) malloc(3* sizeof(double*));
outputArray[0]=(double*) malloc(3* sizeof(double));
for (i=0;i<designs;i++){
outputArray[i]=&outputArray[0][i];
}
Don't forget to free the memory !
free(outputArray[0]);
free(outputArray);
Edit : Contiguous means that you have to allocate the memory for all values at once. See http://www.fftw.org/doc/Dynamic-Arrays-in-C_002dThe-Wrong-Way.html#Dynamic-Arrays-in-C_002dThe-Wrong-Way : some packages, like fftw or lapack require this feature for optimization. As you were calling malloc three times, you created three parts and things went wrong.
If you have a single right hand side, there is no need for a 2D array (double**). outputArray[i] is a double*, that is, the start of the i-th row ( row major). The right line may be outputArray[i]=&outputArray[0][i*nrhs]; if you have many RHS.
By doing this in your code, you are building a 3 rows, one column, that is one RHS. The solution, is of size n=2. It should be outputArray[0][0] , outputArray[1][0]. I hope i am not too wrong, check this on simple cases !
Bye,

Error message: pointer to function

I get an error message saying expression must have (pointer-to-) function type. what am i doing wrong? i just started coding, i know i suck lol. I don't understand how to get the formula for the distance to work.
#include <cmath> //headerfile
#include <iomanip>
#include <iostream>
enter code here
using namespace std;
int main()
{
double d;
double t;
double g;
char choice ='y';
//output numbers to console
while (choice == 'y' || choice =='Y')
{
cout<<"Please input a value for the time"<< endl<<endl;
cin>>t;
g = 32;
d = (g)(t*t);
if (t<0)
cout<<"You cannot have a negative time"<<endl<<endl;
else
cout<<setw(8)<<fixed<<setprecision(2)<<"\n""The distance the ball has fallen is "<<d<<" feet"<<endl<<endl;
cout<<"Would you like to run this again? y for yes, any other key for no."<< endl<<endl;
cin>>choice;
cout<<endl;
}
system ("Pause");
return 0;
}
If (g)(t*t) is supposed to be a normal multiplication operation, then it should be g*t*t.
In your code, g is a double, but you are using it as if it was a pointer to a function (d = (g)(t*t);). If what you really want is to multiply t*t by g, you forgot an *:
d = (g)*(t*t);

Get certain parts out of a string using C

Evening everyone hope on of you gurus can help. I am trying to find the answer to this issue I need to read the data out of the string below by searching the tags. i.e IZTAG UKPART etc however the code I am using is no good as it only stores the 1st part of it for example UKPART = 12999 and misses out the -0112. Is there a better way to search strings ?
UPDATE SO FAR.
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main ()
{
// in my application this comes from the handle and readfile
char buffer[255]="TEST999.UKPART=12999-0112...ISUE-125" ;
//
int i;
int codes[256];
char *pos = buffer;
size_t current = 0;
//
char buffer2[255];
if ((pos=strstr(pos, "UKPART")) != NULL) {
strcpy (buffer2, pos); // buffer2 <= "UKPART=12999-0112...ISUE-125"
}
printf("%s\n", buffer2);
system("pause");
return 0;
}
NOW WORKS BUT RETURN WHOLE STRING AS OUTPUT I NEED TO JUST RETURN UKPART FOR EXAMPLE THANKS SO FAR :-)
strstr() is absolutely the right way to search for the substring. Cool :)
It sounds like you want something different from "sscanf()" to copy the substring.
Q: Why not just use "strcpy ()" instead?
EXAMPLE:
char buffer[255]="IZTAG-12345...UKPART=12999-0112...ISUE-125" ;
char buffer2[255];
if ((pos=strstr(pos, "UKPART")) != NULL) {
strcpy (buffer2, pos); // buffer2 <= "UKPART=12999-0112...ISUE-125"

Resources