Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I want to use printf() in my code

+1 vote
asked Nov 26, 2018 by Nibal Tinawi (190 points)
#include<iostream>

#include<vector>

#include<string>

#include<stdio.h>

using namespace std;

vector<string>sort_strings(vector<string>strings){

int smallest=0;

for(int i=0;i<strings.size();i++){

 for(int j=i;j<strings.size();j++){

if(strings[j].compare(strings[smallest])<0)

{smallest=j;}}

if(smallest!=i){

swap(strings[i],strings[smallest]);}}

return strings;}

int main(){

vector<string> s={"ISE","BSCE","BMC","BL","EDUC","BACT"};

cout<<" The programs names Before sort:"<<endl;

for(int i=0;i<s.size();i++){

printf()(s[i]);}

vector<string> sorted_strings=sort_strings(s);

cout<<"\n The programs names after sort:"<<endl;

for(int i=0;i<s.size();i++){

printf()(sorted_strings[i]);

}

return 0;

}
I want to print the string matrix using the print function "printf()" or another print function not using "cout"
I tried to do this but errors appeared in the code editor
So that the elements of the matrix appear under each other

thank you very much

1 Answer

0 votes
answered Nov 27, 2018 by Юрий Гуреев (1,100 points)
selected Dec 2, 2018 by Nibal Tinawi
 
Best answer

Syntax of printf is

printf("...", …);

So, you need to write something like this:

printf("%s", sorted_strings[i]);

commented Dec 2, 2018 by Nibal Tinawi (190 points)
thank you very much
but I wrote
#include<iostream>

#include<vector>

#include<string>

#include<stdio.h>

using namespace std;

vector<string>sort_strings(vector<string>strings){

int smallest=0;

for(int i=0;i<strings.size();i++){

 for(int j=i;j<strings.size();j++){

if(strings[j].compare(strings[smallest])<0)

{smallest=j;}}

if(smallest!=i){

swap(strings[i],strings[smallest]);}}

return strings;}

int main(){

vector<string> s={"ISE","BSCE","BMC","BL","EDUC","BACT"};

cout<<" The programs names Before sort:"<<endl;

for(int i=0;i<s.size();i++){

printf("%s", sorted_strings[i]);}

vector<string> sorted_strings=sort_strings(s);

cout<<"\n The programs names after sort:"<<endl;

for(int i=0;i<s.size();i++){

printf("%s", sorted_strings[i]);

}

return 0;

}
and this error appeares
main.cpp:37:14: error: ‘sorted_strings’ was not declared in this scope
commented Dec 2, 2018 by Юрий Гуреев (1,100 points)
I wrote ...sorted_strings[i].. fo example. You shoud use your own vector name. In your programm it named sort_strings. So use it instead of  sorted_strings
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...