Jump to content
  • Advertisement
Sign in to follow this  
iamcreasy

10082

This topic is 3016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

m trying to solve ACM - 10082 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=31&page=show_problem&problem=1023 Judge is responding wrong answer...but, i have no idea why. Any suggestion?
#include<iostream>
#include<string>
using namespace std;

char change(char x);

int main()
{
    string str1;

    getline(cin, str1, '\n');

    transform(str1.begin(), str1.end(), str1.begin(), change);
    cout << str1;

    return 0;
}
char change(char x)
{
    //---first row----------
    if(x == '1') return 96;
    if(x == '2') return '1';
    if(x == '3') return '2';
    if(x == '4') return '3';
    if(x == '5') return '4';

    if(x == '6') return '5';
    if(x == '7') return '6';
    if(x == '8') return '7';

    if(x == '9') return '8';
    if(x == '0') return '9';
    if(x == '-') return '0';

    if(x == '=') return '-';

    //----2nd row------------

    if(x == 'Q') return 9;
    if(x == 'W') return 'Q';
    if(x == 'E') return 'W';

    if(x == 'R') return 'E';
    if(x == 'T') return 'R';
    if(x == 'Y') return 'T';

    if(x == 'U') return 'Y';
    if(x == 'I') return 'U';
    if(x == 'O') return 'I';

    if(x == 'P') return 'O';
    if(x== '[') return 'P';
    if(x== ']') return '[';
    if(x== '\\') return ']';

    //----3rd row------------

    if(x== 'S') return 'A';
    if(x== 'D') return 'S';
    if(x== 'F') return 'D';

    if(x== 'G') return 'F';
    if(x== 'H') return 'G';
    if(x== 'J') return 'H';

    if(x== 'K') return 'J';
    if(x== 'L') return 'K';
    if(x== '\;') return 'L';
    if(x== '\'') return '\;';

    //---4th row-------------

    if(x== 'X') return 'Z';
    if(x== 'C') return 'X';
    if(x== 'V') return 'C';

    if(x== 'B') return 'V';
    if(x== 'N') return 'B';
    if(x== 'M') return 'N';

    if(x== ',') return 'M';
    if(x== '.') return ',';
    if(x== '/') return '.';

    if(x == ' ') return ' ';
}

one more thing, is there any way to get ride of this if-return repeatation.

Share this post


Link to post
Share on other sites
Advertisement
Are you handling the multiple lines of input?

You could replace the huge if-else bit with a switch statement, or better still, use a std::string for each row. You can then find each character, and use the one to the left of it.

Share this post


Link to post
Share on other sites
Thanks...

yes, it is not taking several line...But how do i take several line in this manner?

Quote:

use the one to the left of it.


How would i know which ones are adjacent.There is no pattern.And isn't using switch is almost the same as this if-return.It will also take one statement per character.

Share this post


Link to post
Share on other sites
I think what mattd was suggesting is something like (off the top of my head):

char tmp[2];
tmp[0]=x;
tmp[1]=0;
string inStr(tmp);
string keyStr = "~1234567890";

int pos = keyStr.find(inStr);
if( pos != string::npos && pos > 0) return keyStr[pos-1];
// don't know what you do with '~'
... // and so on for other rows
... // keyStr can also be a vector of string or just a string of the entire
// keyboard, I suppose

Share this post


Link to post
Share on other sites
Quote:
Original post by iamcreasy
yeah...interesting.

But, im still wondering, how to take multiple line as input, while using string class?


#include <iostream>
#include <string>

using namespace std;

string process(const string& str) {
return "Processed: " + str;
}

int main() {
while(cin) {
string line;
getline(cin, line);

cout << process(line) << "\n";
}
}

Share this post


Link to post
Share on other sites
Thanks a lot...the problem is almost solved...but a little problem has appeared.

The following code is supposed to print three lines from "in.txt" file(which contains three lines).But it is printing the last line twice.The size method of vector is also showing one extra count of the total line amount.

s_v[3] and s_v[4] is showing the same result.where s_v[4] dont even exists in "in.txt"

How am i supposed to get ride of this problem?


#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
freopen("in.txt", "r", stdin);

vector<string> s_v;
string str;

while(cin)
{
getline(cin, str);
s_v.push_back(str);
}

for(int i=0; i<s_v.size(); i++)
{
cout << s_v << endl;
}

return 0;
}



also, i wanna get ride of this line


freopen("in.txt", "r", stdin);


I used it when i coded in C.What should i use in C++?

Share this post


Link to post
Share on other sites
Quote:
Original post by iamcreasy
Thanks a lot...the problem is almost solved...but a little problem has appeared.

The following code is supposed to print three lines from "in.txt" file(which contains three lines).But it is printing the last line twice.The size method of vector is also showing one extra count of the total line amount.

Oops... use while(cin.good()), not while(cin).

Quote:

also, i wanna get ride of this line


freopen("in.txt", "r", stdin);


I used it when i coded in C.What should i use in C++?

Something like:

ifstream file("in.txt");
cin.rdbuf(file.rdbuf());

Share this post


Link to post
Share on other sites
This line is generating the following error..


ifstream file("in.txt");


error: variable `std::ifstream file' has initializer but incomplete type

:(

Share this post


Link to post
Share on other sites
Did you #include <fstream>?

Also, it seems your overcomplicating it a bit.

ifstream f("in.txt");
string str;
for(int i=0; i<3; i++)
{
getline(f,str,'\n');
cout << str.c_str() << endl;
}

Am I missing something?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!